vscode working directory when debugging python
Asked Answered
C

4

11

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

Chapatti answered 5/5, 2017 at 9:20 Comment(0)
B
13

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)

Brno answered 22/9, 2017 at 10:8 Comment(0)
H
7

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}"
        }
    ]
}
Holograph answered 5/3, 2021 at 21:21 Comment(0)
P
1

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.

Powdery answered 21/4, 2020 at 21:17 Comment(0)
Z
0

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}"
        }
    ]
}
Zarah answered 8/1 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.