23

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.

3
  • That configuration you linked should achieve that, have you tried it? did it work?
    – bracco23
    Jun 26, 2019 at 15:46
  • @bracco23 I don't know how to set it up. Is it under .vscode folder in root directory? Jun 26, 2019 at 22:51
  • did you figure this out without having to modify the debugging launch.json? I literally just want to run a python script and have it execute in the folder it resides in...I thought this was pretty basic?
    – Derek Eden
    Apr 27, 2020 at 14:34

9 Answers 9

20

Modify this setting:

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

4
  • 4
    "cwd": "${fileDirname}" in launch.json did not work for me. But this did. Now current working directory is set to the file location. Thank you!
    – Jake
    Sep 9, 2020 at 13:33
  • 4
    Is there a way to do this for Debug as well?
    – Shep Bryan
    May 7, 2022 at 18:46
  • 1
    There's no "Python" under Settings here. Aug 28, 2022 at 15:59
  • 4
    @ShepBryan this helped me to set it for debug as well: github.com/microsoft/vscode-python/issues/…
    – dogekali
    Dec 9, 2022 at 9:24
13

Updated Solution: working as of 21/1/2021

Option 1:

  1. Locate and open the user settings file:
    • Windows %APPDATA%\Code\User\settings.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.
2
  • 2
    Sounds good in theory but it doesn't work. Shift+Enter still launches python in my home dir instead of the script dir.
    – Fizz
    Feb 11, 2022 at 12:32
  • 2
    As of august 2022, none of these work. Aug 28, 2022 at 16:05
8

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": ""
5
  • "cwd": "" was the magic fix for me, thanks…
    – ptim
    May 24, 2021 at 7:00
  • …actually, also required me to manually cd into $fileDirname before running the script with "console": "integratedTerminal"
    – ptim
    May 24, 2021 at 7:08
  • The wording of this answer is confusing. "... just create a launch.json file a .vscode subfolder ..." Is it one, the other, or both? Aug 28, 2022 at 16:09
  • @EzequielBarbosa missing a preposition. you should have .vscode/launch.json at the end, create whatever is missing :)
    – bracco23
    Aug 29, 2022 at 10:43
  • This works but you have to do this for every new project you start on. Isn't there a way to make this setting global? I've tried adding these settings in ~/.vscode/launch.json instead but no luck.
    – Jakob
    Mar 2 at 10:08
7

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.
4
  • 1
    Checked but it doesn't work even after restarting VSCode. Typical MSFT stuff.
    – Fizz
    Feb 11, 2022 at 12:35
  • 2
    Apparently you have to open the dir as a workspace in code, not just a script file github.com/microsoft/vscode-python/issues/… Who does that? Total rubbish.
    – Fizz
    Feb 11, 2022 at 12:42
  • For me it was working, I'm sorry if It didn't work for you.
    – Daniel
    Feb 12, 2022 at 13:13
  • This worked for me as well.
    – Portfedh
    Feb 13, 2022 at 1:33
4

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.

2

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

1
  • This worked for me as well, when none of the other ones did.
    – Astrid
    Feb 28 at 23:55
1

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.

0

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.

0

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

5
  • As of [28/02/2023] there is no preferences under file.
    – Astrid
    Feb 28 at 23:53
  • Yes, there is today (01/03/2023) on VSCode 1.75.1. For the sake of usability, I have added the default shortcut to "Settings". Official docs from 2/2/23: code.visualstudio.com/docs/getstarted/settings Not sure why you are missing yours. Mar 1 at 13:18
  • How utterly bizarre. I genuinely don't have that option under File. I am on a Mac and on the same version 1.75.1. That is very odd.
    – Astrid
    Mar 1 at 18:48
  • 1
    You have it under Code on Mac @Astrid. The rest of the steps are the same.
    – Jakob
    Mar 2 at 9:24
  • Apologies for assuming Windows. Anyway, as an update: code.visualstudio.com/docs/getstarted/settings from 3/1/2023 on VScode 1.76.0. But also, if you had opened the link (and potentially scrolled down once, depending on the size and resolution of your screen) you would have seen the set of instructions for "On Windows/Linux" and "On macOS". Obviously, I'm not capable of explaining why it's different for Win/Lin and Mac. Out of curiosity, does the shortcut Ctrl+, (comma) work for you? Macs don't have Ctrl though, do they; it's Cmd perhaps? Mar 2 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.