How to debug python in VS Code with virtualenv? Again

Viewed 5441

I'm trying to debug a very simple script in a venv, and the Python debugger just refuses to start working.

The Python extension version is 2019.10.41019 on Windows 10 x64.

The python version is 3.7.1 32-bit.

Debugging without virtual environments works fine. Then I created a venv in C:\TMP\PYENV and added a configuration in launch.json:

{
    "name": "Python: PYENV",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "pythonPath": "C:\\TMP\\PYENV\\Scripts\\python.exe",
},

and start debugging it. Output in terminal:

c:\TMP\PYENV>C:/TMP/PYENV/Scripts/activate.bat
(PYENV) c:\TMP\PYENV>C:\TMP\PYENV\Scripts\python.exe c:\Users\user\.vscode\extensions\ms-python.python-2019.10.41019\pythonFiles\ptvsd_launcher.py --default --client --host localhost --port 53150 c:\TMP\PYENV\myscript.py

The script starts, all local imports in the venv are found - but all my breakpoints are ignored. The script runs through (and crashes somewhere).

What is missing??

I've searched related questions and most advice to define a config with "python.pythonPath". But this is not valid anymore in the current version. It should be "pythonPath" as in my example above, or the VS Code (or python extension?) complains about invalid name.

1 Answers

It's because you put your source code within the virtual environment which makes the debugger think it isn't your code but third-party code. Simply move the code out and it will be fine. Alternatively you can set "justMyCode": true in your debugger config and it will then trace through all code and not just what the debugger considers your own code.

Related