121

I'm starting to use vscode for Python. I have a simple test program. I want to run it under debug and I need to set the working directory for the run.

How/where do I do that?

user1443098
  • 4,196
  • 4
  • 22
  • 45

7 Answers7

188

@SpeedCoder5 's comment deserves to be an answer;

Specifically, you can specify a dynamic working directory; (i.e. whichever directory where the currently-open Python file is located), using "cwd": "${fileDirname}"

If you're using the Python: Current File (Integrated Terminal) option when you run Python, your launch.json file might look like mine, below.

{
    "version": "0.2.0",
    "configurations": [
    {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}"
    }, 

    //... other settings, but I modified the "Current File" setting above ...
}

Remember the launch.json file controls the run/debug settings of your Visual Studio code project; my launch.json file was auto-generated by VS Code, in the directory of my current "Open Project". I just edited the file manually to add "cwd": "${fileDirname}" as shown above.

Remember the launch.json file may be specific to your project, or specific to your directory, so confirm you're editing the correct launch.json (see comment)

If you don't have a launch.json file, try this:

To create a launch.json file, open your project folder in VS Code (File > Open Folder) and then select the Configure gear icon on the Debug view top bar.

The Red Pea
  • 12,346
  • 12
  • 77
  • 112
  • 6
    Any way to set this as a default setting for all configurations? – Serhiy May 17 '19 at 08:01
  • 1
    Be sure you're editing the right launch.json file! In my experience VS Code creates a .vscode directory in every project folder I open. If yesterday you opened folder parent/ and today you opened parent/child/, you'll have to make changes appropriately. – chrisinmtown Oct 26 '20 at 12:55
  • 1
    There is another file that looks similar to launch.json: workspace[x].code-workspace. It has a 'launch' key. Is this setting there similar to launch.json entries? – Timo Nov 30 '20 at 13:33
  • 1
    Can I set the folder in the terminal to the folder the script was?. E.g. my last ps1 script line is set-location foo, I want the terminal to be in foo. – Timo Nov 30 '20 at 14:33
  • 1
    This answer did not work for me, but this one did: https://stackoverflow.com/a/62331298/65326 – Apreche Dec 31 '20 at 15:50
  • What the heck is a launch.json file? Instructions make no sense. – Benbob Apr 19 '21 at 03:27
  • @Benbob, the launch.json file controls the run/debug settings of your Visual Studio code project; [more info on the launch.json file here](https://code.visualstudio.com/docs/editor/debugging#_launch-versus-attach-configurations) – The Red Pea Apr 19 '21 at 21:05
46

All you need to do is configure the cwd setting in launch.json file as follows:

{
    "name": "Python",
    "type": "python",
    "pythonPath":"python", 
    ....
    "cwd": "<Path to the directory>"
    ....
}

More information about this can be found on the official VS Code docs website.

Addison
  • 5,106
  • 2
  • 31
  • 49
Don
  • 5,896
  • 2
  • 21
  • 32
19

This setting helps me:

{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "cwd": "${workspaceFolder}\\app\\js", // set directory here
  "program": "${workspaceFolder}\\app\\js\\server.js", // set start js here
}
Galak Fyyar
  • 54
  • 3
  • 7
Xin
  • 22,636
  • 12
  • 71
  • 68
13

In some cases, it might be also useful to set the PYTHONPATH along with the workspaceFolder:

{
    "name": "Python: Current File",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "cwd": "${workspaceFolder}",
    "env": {
        "PYTHONPATH": "${cwd}"
    }
}

CermakM
  • 994
  • 1
  • 10
  • 14
5

I am posting this sample configuration for people who use TypeScript on Node.js

in my project my Node.js server TypeScript files are located in folder Application_ts and the compiled js files are generated in the folder named Application

because when we run our application in debug mode or start it normally we should start from Application folder which contains the js files so bellow configuration run debug from root folder where my application_ts also exists and works perfect

{
  "version": "0.2.0",
  "configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Debug TypeScript in Node.js",
        "program": "${workspaceRoot}\\Application\\app.js",
        "cwd": "${workspaceRoot}\\Application",
        "protocol": "inspector",
        "outFiles": [],
        "sourceMaps": true
    },        
    {
        "type": "node",
        "request": "attach",
        "name": "Attach to Process",
        "port": 5858,
        "outFiles": [],
        "sourceMaps": true
    }
 ]
}
MJ X
  • 6,186
  • 8
  • 45
  • 77
1

You can set up current working directory for debugged program using cwd argument in launch.json

Krzysztof Cieslak
  • 1,595
  • 12
  • 14
  • Super! Got me to the next step. Now, trying to actually run the program in debug. here's the code: print (os.getcwd()) – user1443098 Jul 28 '16 at 13:55
  • Super! Got me to the next step. I have a one-liner: print (os.getcwd()) that I want to debug. I click on the debug icon (or hit F5), There's a little blue moving line under Environment pulldown. However, my code never runs. Guess I'm missing something. What would that be? – user1443098 Jul 28 '16 at 13:57
  • forget those last two, I got it – user1443098 Jul 28 '16 at 14:06
  • 1
    Next thing. Using the suggestion, I was surprised to see that the ${workspaceRoot} was not actually the root but the .vscode directory under the root. Is this as expected? If so, I can set cwd to ${workspaceRoot}\.. (on windows anway) – user1443098 Jul 28 '16 at 15:08
0

To set current working directory to whatever file you are executing at the time:

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

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

Jake
  • 53
  • 1
  • 4