stackunderflow
stackunderflow

Reputation: 3873

vscode working directory when debugging python

I have a simple problem regarding debugging python script on vscode .

When I open a parent directory on vs code that contains multiple children directories and then I open multiple python files from these children directories.

I then try to debug these files.

the problem is that from the launch.json the cwd is set up to be the parent folder. But I am now running a script in subfolders. and jumping from subfolder to subfolder.

So changing the "cwd": "workspaceRoot" every now and then isn't practical for me

is there a way that the debugger will always use the current folder of the debugged script file as the current directory??

p.s this question didn't help me stackoverfollow question

Upvotes: 11

Views: 6903

Answers (4)

SpeedCoder5
SpeedCoder5

Reputation: 8978

Per How to make VSCode run your code from the location of the code Add "cwd": "${fileDirname}" in the launch.json file. .vscode/launch.json should look something like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "cwd": "${fileDirname}"
        }
    ]
}

Upvotes: 0

p8R
p8R

Reputation: 101

I had a similar problem: I had a subfolder with Python script and some data file. While debugging a script, I was getting an error with message, that the data file could not be found.

Here is the debug configuration which solved this problem:

{
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${workspaceFolder}\\${relativeFileDirname}"
        }
    ]
}

Upvotes: 7

neves
neves

Reputation: 39153

The other answer didn't solve my problem. It worked if I put a dot in the current working dir configuration in launch.json. Here it is:

  "configurations": [
    {
      "cwd": ".",
      "name": "Python: Current file",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal"
    },
   ]

To open the launch configuration just go to the debug tab and click in the top gear icon.

I'm using VSCode 1.44.2.

Upvotes: 1

L.Rtvri
L.Rtvri

Reputation: 131

try setting "cwd": "", in launch.json,

at least it works in the latest version, I can run python modules from root and subdirectories without changing launch settings (vs.code 1.16.1, python extension 0.7)

Upvotes: 13

Related Questions