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,
- work__assignments (this is my main workspace folder in vs code)
- assignment__1
- data
- modules
- tests
- assignment__2
- data
- modules
- tests
- assignment__1
"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,
- 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}"
}
- Updating the launch.json file and adding the line
"cwd": ""
,
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": ""
}
- 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}"
}
}
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,
CodePudding user response:
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.
A simple example
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}"
}
]
}
Invalid configuration in launch.json
if debug is selected from play button