How to debug a file from current directory with Python in VS Code?

Question:

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

Asked By: exan

||

Answers:

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

Answered By: JialeDu