Cameron Blumenthal
Cameron Blumenthal

Reputation: 93

VS Code Debugger Immediately Exits

I use VS Code for a python project but recently whenever I launch the debugger it immediately exits. The debug UI will pop up for half a second then disappear. I can't hit a breakpoint no matter where it's placed in the current file. The project has the expected normal behavior when run in non-debug mode. I vaguely remember a command being inserted into the terminal window when I used to click debug but now I see nothing. I opened a totally different project but debugger still exits immediately.

Any advice? Anywhere I can find logs for the debugger run?

My launch.json file:

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

I have tried: running app as admin, reinstalling vs code, reinstalling python extension, restarting app, restarting computer, disabling all non-essential extensions, deleting launch.json, launching a file with only print statement.

Upvotes: 9

Views: 4858

Answers (2)

mchristos
mchristos

Reputation: 1939

In my case I had a "publish" script who's main method was called publish (not main) and there was no if __name__ == "__main__": expression in the script. This was because my function was called via a poetry script in the form

[tool.poetry.scripts]
publish = "scripts.publish:publish"

i.e. the script was run via poetry run publish

Of course, it's probably possible to invoke poetry from the debug configuration, but the most common way is to invoke the file itself, like this

        {
            "name": "publish",
            "type": "debugpy",
            "request": "launch",
            "program": "${workspaceFolder}/scripts/publish.py",
            "console": "integratedTerminal",
            "justMyCode": true,
        },

Of course this won't work since publish.py just defines a publish function, and is not invokable as a script!

The solution is to add a clause if __name__ == "__main__": which invokes the main script function!

Upvotes: 0

JialeDu
JialeDu

Reputation: 9903

Please install Python 3.7 or later.

If you must use Python 3.6 or earlier, rollback the Python extension to version 2022.08.0.

Screenshot of extension in VS Code with "Install Another Version..." highlighted from the "Uninstall" alternate menu

Screenshot of extension version list with "2022.8.1" and "2022.8.0" highlighted

Upvotes: 7

Related Questions