12

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,698
  • 3
  • 12
  • 32
Fawwaz Yusran
  • 1,093
  • 2
  • 15
  • 32
  • That configuration you linked should achieve that, have you tried it? did it work? – bracco23 Jun 26 '19 at 15:46
  • @bracco23 I don't know how to set it up. Is it under .vscode folder in root directory? – Fawwaz Yusran Jun 26 '19 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 '20 at 14:34

6 Answers6

13

I think the setting you need is here:

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

brch
  • 189
  • 2
  • 7
10

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
  • 124
  • 1
  • 2
  • 1
    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 '22 at 12:32
7

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 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": ""
Aren Cambre
  • 6,315
  • 9
  • 28
  • 35
bracco23
  • 2,104
  • 9
  • 25
  • `"cwd": ""` was the magic fix for me, thanks… – ptim May 24 '21 at 07:00
  • …actually, also required me to manually `cd` into `$fileDirname` before running the script with `"console": "integratedTerminal"` – ptim May 24 '21 at 07:08
4

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
  • 991
  • 2
  • 13
  • 36
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
  • 32
  • 4
0

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,303
  • 1
  • 23
  • 48