Reputation: 73
I am trying to run python code inside Vs code, when I click "Run Python File" Option from top right (play icon). It shows "FileNotFoundError: [Errno 2] No such file or directory: 'jpgs'"
Upon using "pwd" command in same terminal, it shows It's in "/Users/arsh" Directory instead of my python project directory. I have to cd to that folder and run the button to make it working.
Is there any way in VS code to run the python file from current folder automatically? So every time I don't need to tell terminal to goto project folder then run "Run python code" command from VS Code.
Upvotes: 6
Views: 16278
Reputation: 21
You can also change the VSCode Settings/Features/Terminal "Terminal › Integrated: Cwd" setting to use the current folder, however it was opened.
Set it to ${workspaceFolder}
.
It's puzzling why this isn't the default. Make sure you kill all active terminals and exit VSCode.
Upvotes: 2
Reputation: 2425
I found that the easiest way was to right click on the dir (or a file in that dir) in the explorer view and select "Open in Integrated Terminal"
Upvotes: 0
Reputation: 13964
In addition to the answers posted already, consider assigning a keyboard shortcut as follows in your keybindings.json
:
{
"key": "cmd+shift+alt+t",
"command": "workbench.action.terminal.newWithCwd",
"args": {
"cwd": "${fileDirname}"
}
}
This will open a terminal in the folder containing the active file.
Upvotes: 5
Reputation: 11
I think VS Code will often run scripts in the Python install location. You can change this.
Click the gear icon on the bottom left of VS Code.
Then click settings.
A search bar will come up. Type in "Execute in File Path" and click the checkmark or set that to true.
Reload Visual Studio Code and you shouldn't have to open Visual Studio Code in the folder each time you want to run a script without specifying the absolute file path.
Upvotes: 1
Reputation: 4113
Open the project folder in VS Code using File -> Open Folder...
. This will make the shell always start in that folder. You can then re-open your Python file from within that new workspace.
Upvotes: 4