Search code examples
pythonvisual-studio-codevscode-debuggerpython-venv

`.venv` ignored when debugging Python in VSCode under Windows


I've a Python project in Windows which provides a command line application via console_script. The Python package uses a poetry virtualenv in <project-root>/.venv and is installed into the venv in editable mode.

Now I'd like to debug the command line application in the integrated VSCode terminal. To beeing able to do so I created a launch.json config like follows:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Device Agent (WLAN)",
            "type": "python",
            "request": "launch",
            "program": "mvp-end-device-agent",
            "args": ["<", "-r", "D:\\MassHunter\\Data\\demo_0000.d"],
            "console": "integratedTerminal"
        }
    ]
}

If I start the debugger a new terminal is opened and I get the following error:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.


PS D:\mvp-end-device-agent>  & 'd:\mvp-end-device-agent\.venv\Scripts\python.exe' 'c:\Users\Plasmion\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '58875' '--' 'mvp-end-device-agent' '<' '-r' 'D:\MassHunter\Data\demo_0000.d'
Traceback (most recent call last):
  File "d:\python\python386\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "d:\python\python386\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\Plasmion\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\Plasmion\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\Plasmion\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 267, in run_file
    runpy.run_path(options.target, run_name=compat.force_str("__main__"))
  File "d:\python\python386\lib\runpy.py", line 264, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "d:\python\python386\lib\runpy.py", line 234, in _get_code_from_file
    with io.open_code(decoded_path) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\mvp-end-device-agent\\mvp-end-device-agent'
PS D:\mvp-end-device-agent> & d:/mvp-end-device-agent/.venv/Scripts/Activate.ps1
(.venv) PS D:\mvp-end-device-agent>

It seems like the venv is not activated before executing the command line application but afterwards. According to VSCode docs - Where the extension looks for environments and microsoft/vscode-python/issues/8372 poetry venvs are hardly supported ATM.

I already tried to specify the venv Python environment manually by adding

{
    "python.pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe"
}

to the project local .vscode/settings.json file without success.

How can I fix or workaround this?


Solution

  • It turned out that the issue related not to venv management but to how console_scripts have to be handled w.r.t. debugging: https://stackoverflow.com/a/64558717/5308983