Python in VSCode: Set working directory to python file's path everytime

Question:

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.

Asked By: Fawwaz Yusran

||

Answers:

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": ""
Answered By: bracco23

Modify this setting:

File > Preferences > Settings > Python > Data Science > Execute in File Dir

Answered By: brch

Updated Solution: working as of 21/1/2021

Option 1:

  1. Locate and open the user settings file:
    • Windows %APPDATA%CodeUsersettings.json
    • macOS $HOME/Library/Application Support/Code/User/settings.json
    • Linux $HOME/.config/Code/User/settings.json
  2. Add this line: "python.terminal.executeInFileDir": true

Option 2:

  1. Open the Settings editor:
    • On Windows/Linux – File > Preferences > Settings
    • On macOS – Code > Preferences > Settings
    • or use the keyboard shortcut (Ctrl+,).
  2. Check the following box:
    • Extensions > Python > Terminal: Execute In File Dir.
    • or use the Search bar and type this setting id python.terminal.executeInFileDir.
Answered By: Dr. S

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.

Answered By: sprico27

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.
Answered By: Daniel

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.

Answered By: Fizz

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+,".

Answered By: Justin Case

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__))

Answered By: Rogério Gouvêa

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"]
}

Source.

Answered By: guatavita