Ben's notes

Debugging Python in Vscode: The ideal setup

Because of the way python’s import system “works” (see previous post) I have created a custom debugging launch.json configuration in vscode for when I want to run a single file in a subpackage (also works with a file in the root folder).

To use this configuration you will need the Command Variable (repo) extension.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "cwd": "${command:extension.commandvariable.workspace.folder1Up}",
            "module": "${workspaceFolderBasename}.${command:extension.commandvariable.file.relativeFileDotsNoExtension}",
            "justMyCode": true, // not strictly required
        },
    ]
}

Now when you debug this will set the working directory to the parent of the workspace folder and then run the current file as a module (without the .py extension) with the dot path relative to the workspace folder. It’s complicated but it will mean that relative imports work and that you are debugging similar to how the file will be run when it’s part of a larger imported module. See previous post for a detailed explanation on why this is necessary.