VSCode中PS/CMD终端不打开当前工作目录及Python运行问题求助
Hey there, let's work through these VSCode terminal and Python issues together—they're pretty common, so we'll get you sorted out in no time!
First, let's tackle the core issue of your terminal not landing in your workspace folder. Try these steps one by one:
Check your terminal working directory setting
Open VSCode settings (pressCtrl+,), search forterminal.integrated.cwd, and make sure it's set to${workspaceFolder}. This variable tells VSCode to launch the terminal directly in your open workspace directory. If it was changed to a hardcoded path, switch it back to this dynamic variable.Reset your default terminal profile
Sometimes corrupted profile settings throw this off. PressCtrl+Shift+P, typeTerminal: Select Default Profile, and reselect either PowerShell or Command Prompt. Then runTerminal: Create New Integrated Terminalfrom the command palette to test if it opens in the right directory.Run VSCode as Administrator
Permissions can block VSCode from accessing certain directories. Right-click the VSCode icon, select "Run as administrator", then open your workspace and launch a new terminal. This often fixes access-related directory issues.Verify system PATH variables
Ensure your system's PATH includes the default locations for CMD and PowerShell:- CMD path:
C:\Windows\System32\cmd.exe - PowerShell path:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
If either is missing, add them to your system PATH via Windows Settings > System > About > Advanced system settings > Environment Variables.
- CMD path:
Once your terminal is opening in the correct folder, if Python still won't run there, try these tweaks:
Confirm the terminal is in the right directory
In PowerShell, runpwd; in CMD, runcdto check the current path. It should match your workspace folder. If not, manually navigate there withcd path/to/your/workspacebefore running your script.Select the correct Python interpreter
PressCtrl+Shift+P, typePython: Select Interpreter, and pick the Python version (or virtual environment) you want to use for your project. VSCode will then tie this interpreter to your terminal sessions, ensuring it can access your workspace files.Check Python's accessibility in the terminal
In the terminal, runpython --version(orpy --versionfor the Windows Python launcher). If it throws an error, your Python installation path might not be in the system PATH. Add your Python directory (e.g.,C:\Python311) and itsScriptssubfolder to the system PATH, then restart VSCode.Move your project to a non-restricted directory
If your workspace is in a protected folder likeC:\Program Files, Python might lack permission to read/write files. Move your project to a user directory (e.g.,C:\Users\YourName\Projects) and try again.
内容的提问来源于stack exchange,提问作者Abhiseck

