0

I can see that this question has been asked multiple times already however, none of the solutions seems to be working anymore, forcing me to ask for the latest fix.

Problem

I have created a workspace in VS code with the following directory structure,

VS Code directory structure

  • work__assignments (this is my main workspace folder in vs code)
    • assignment__1
      • data
      • modules
      • tests
    • assignment__2
      • data
      • modules
      • tests

"work__assignments" is my main workspace folder in the VS code. From this main workspace, I go to an assignment folder (e.g. work__assignments > assignment__2 > modules) and work on the respective code. However, when I try to debug the code in "work__assignments > assignment__2 > modules" the debugger loads from the main workspace folder (i.e. work__assignments), then it fails because it can't find other modules in the modules folder "work__assignments > assignment__2 > modules".

I have tried the following methods so far,

  1. Updating the launch.json file and adding the line "cwd": "${fileDirname}",
{
    "name": "Python: Current File",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "cwd": "${fileDirname}"
}
  1. Updating the launch.json file and adding the line "cwd": "",
{
    "name": "Python: Current File",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "cwd": ""
}
  1. Updating the launch.json file and adding the following lines,
{
    "name": "Python: Current File",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "cwd": "${workspaceFolder}",
    "env": {
        "PYTHONPATH": "${cwd}"
    }
}
  1. Updating the preferences

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

However, NONE OF THESE APPROACHES HAVE WORKED. Every time I debug the code, it launches into the workspace folder and not in the code folder. What am I doing wrong?

Note: I am running the debugger using the "Debug Python File" button at the top right corner as shown in the picture below, enter image description here

1
  • Try setting cwd to "cwd": "./assignment__2". Commented Sep 29, 2022 at 2:33

1 Answer 1

5

Configure one of the following cwd in launch.json:

    "cwd": "./assignment__2"
    "cwd": "${fileDirname}"

It should be noted that you need to debug the program from the Run and Debug panel to execute the configuration in launch.json.

enter image description here

A simple example

Directory Structure and code enter image description here

launch.json

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

Debug results enter image description here

Invalid configuration in launch.json if debug is selected from play button enter image description here

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

4 Comments

ahhh... turns out I was using the wrong button to run the "debugging". Never knew that there is this other button to run the debugging session.
So, is there a way to edit the config of the "Debug Python File" button?
@Matthieu H It seems that it is not feasible for the time being. But there are several related GitHub links here you can follow. [github.com/microsoft/vscode-python/issues/20746] [github.com/microsoft/vscode-python/issues/11812] [github.com/microsoft/vscode-python/issues/20269]
@JialeDu Yes, this is faisable. You just have to set the purpose in launch.json, on the config you want the button to execute. Like this: "purpose": ["debug-in-terminal"]

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.