25

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.

Nordle
  • 2,915
  • 3
  • 16
  • 34
Fawwaz Yusran
  • 1,260
  • 2
  • 19
  • 36

9 Answers9

23

Modify this setting:

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

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
brch
  • 407
  • 4
  • 7
16

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.
Dr. S
  • 184
  • 1
  • 2
9

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": ""
bracco23
  • 2,181
  • 10
  • 28
  • …actually, also required me to manually `cd` into `$fileDirname` before running the script with `"console": "integratedTerminal"` – ptim May 24 '21 at 07:08
  • The wording of this answer is confusing. "... just create a launch.json file a .vscode subfolder ..." Is it one, the other, or both? – Ezequiel Barbosa Aug 28 '22 at 16:09
  • @EzequielBarbosa missing a preposition. you should have `.vscode/launch.json` at the end, create whatever is missing :) – bracco23 Aug 29 '22 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 02 '23 at 10:08
8

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.
Daniel
  • 1,398
  • 4
  • 20
  • 47
  • 2
    Apparently you have to open the dir as a workspace in code, not just a script file https://github.com/microsoft/vscode-python/issues/1062#issuecomment-372794910 Who does that? Total rubbish. – Fizz Feb 11 '22 at 12:42
  • For me it was working, I'm sorry if It didn't work for you. – Daniel Feb 12 '22 at 13:13
5

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.

guatavita
  • 86
  • 1
  • 3
  • the `"purpose": ["debug-in-terminal"]` was the thing that made it work as I was going down this page trying everything! – Spangen Jun 26 '23 at 10:58
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.

Fizz
  • 4,782
  • 1
  • 24
  • 51
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__))

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.

sprico27
  • 42
  • 4
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+,".

  • As of [28/02/2023] there is no preferences under file. – Astrid Feb 28 '23 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: https://code.visualstudio.com/docs/getstarted/settings Not sure why you are missing yours. – Justin Case Mar 01 '23 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 01 '23 at 18:48
  • 1
    You have it under Code on Mac @Astrid. The rest of the steps are the same. – Jakob Mar 02 '23 at 09:24
  • Apologies for assuming Windows. Anyway, as an update: https://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? – Justin Case Mar 02 '23 at 12:45