6

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

Community
  • 1
  • 1
stackunderflow
  • 3,539
  • 4
  • 24
  • 38

3 Answers3

7

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)

L.Rtvri
  • 71
  • 2
  • 4
2

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}"
        }
    ]
}
p8R
  • 21
  • 1
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.

neves
  • 20,547
  • 15
  • 108
  • 137