Post

Step debugging python in vscode

I’ve struggled a lot in making python debugging work in vscode, so I’m writing this post to help others (and myself in the future).

While trying to step debug Barman, I’ve found that VSCode was not able to find the of the modules I was trying to debug, and, no matter where i was trying to set the PYTHONPATH environment variable to the root of the project or in the vscode settings, the debugger was not able to find the source code.

As stupid as I may sound for not having thought about it before, the solution was to add the PYTHONPATH variable in the env section of the launch.json file, with value "${workspaceRoot}". This will point VSCode to the project root, allowing the correct loading of your modules.

1
2
3
4
5
6
7
8
9
10
11
12
13
        {
            "name": "<name>",
            "type": "python",
            "request": "launch",
            "program": "<path to the python file you want to run>",
            "console": "integratedTerminal",
            "env": {
                "PYTHONPATH": "${workspaceRoot}"
            },
            "args": [
                "<args to pass to the python file>"
            ]
        }
This post is licensed under CC BY 4.0 by the author.