简体   繁体   中英

`.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?

That's not a bug. The default Terminal prefix should be PS \\the current working directory\\ , so when we start debugging, it will open a new terminal instance which shows the default prefix. However when we look at its execution scripts:

在此处输入图片说明

The first one is the .venv\\python.exe which we select it as interpreter, after debugging finished, the environment was activated. Then you choose debug again, this terminal will keep .venv being activated.

事实证明,问题与 venv 管理无关,而是与如何处理console_script s 相关,调试: https ://stackoverflow.com/a/64558717/5308983

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM