Reputation: 1480
So I want to migrate from Spyder to VSCode, and I come across this problem where I cannot access a dataset as my working directory is not the same as the dataset's path.
launch.json
is not auto-generated for me as I'm not debugging anything (I tried this).
How do I set the working directory to always be the dir of the Python file I want to run, in VSCode? (and if it's bad practice, could you show me a config that is easy to work with?) I'd like to set this up for VSCode's IPython terminal.
Upvotes: 38
Views: 80424
Reputation: 11
What worked for me (16/01/2023) is going to File > Preferences > Settings and I just started typing "execute file in" in the 'Search settings' field and the top result was "Python › Terminal: Execute In File Dir". Can't comment on the debug environment. Note: Settings can be accessed by using shortcut "Ctrl+,".
Upvotes: 1
Reputation: 106
This worked for me:
include this in your /.vscode/launch.json
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false,
"cwd": "${fileDirname}",
"purpose": ["debug-in-terminal"]
}
Upvotes: 7
Reputation: 183
I tried all these solutions to no effect. My problem was just to access files from the current folder in python, I solved doing in the beginning of the script:
os.chdir(os.path.dirname(__file__))
Upvotes: 5
Reputation: 2211
You can find more details on the launch.json
setting file in the Visual Studio Code User Guide, included how to create one and what it means.
In short, you should be able to just create a launch.json
file in a .vscode
subfolder of the directory you usually open with Open Worspace and paste the snippet provided by the other answer. If you find that it doesn't work, you can try changing the cwd
option going from this:
"cwd": "${fileDirname}"
to this
"cwd": ""
Upvotes: 10
Reputation: 517
Modify this setting:
File > Preferences > Settings > Python > Data Science > Execute in File Dir
Upvotes: 34
Reputation: 5203
Mind you the solutions on this page don't work unless you open the dir as a workspace in Code. If you just open the script, none of these answers work.
MSFT doesn't see this as an issue worth fixing. Because everyone on their island works in workspaces not with scripts, even with scripting languages.
Upvotes: 5
Reputation: 1660
Updated Solution working as of 24th of January 2022
It can be changed in the Settings menu. Go to File > Preferences > Settings and Search for "Execute in File Path". You will find a option which is called:
Python > Terminal: Execute In File Dir
When executing a file in the terminal, whether to use execute in the file's directory, instead of the current open folder.
Upvotes: 8
Reputation: 42
Add the following settings to your settings.json
"python.terminal.executeInFileDir": true,
"code-runner.fileDirectoryAsCwd": true
To Dr. S's solution I added the "code-runner.fileDirectoryAsCwd": true
setting from the Code Runner extension. The first setting sets the working directory to the python file path only if it is run in the terminal. However, the working directory will revert to the root directory if the code is run in the Output tab with CTRL+ALT+N
. This may also be the reason why any settings in the launch.json
file such as "cwd": "${fileDirname}"
do not work, as I have tried as well. The second setting solves this, which allows you to set the working directory to the python file's path even when you choose to run code outside of the terminal.
Upvotes: 0
Reputation: 374
Updated Solution: working as of 21/1/2021
Option 1:
%APPDATA%\Code\User\settings.json
$HOME/Library/Application Support/Code/User/settings.json
$HOME/.config/Code/User/settings.json
"python.terminal.executeInFileDir": true
Option 2:
(Ctrl+,)
.python.terminal.executeInFileDir
.Upvotes: 25