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

4 Answers 4

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)

Sign up to request clarification or add additional context in comments.

Comments

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}"
        }
    ]
}

Comments

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.

Comments

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}"
        }
    ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.