在 Windows 下的 VSCode 中调试 Python 时忽略 `.venv`

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

我在 Windows 中有一个 Python 项目,它通过 console_script 提供命令行应用程序。 Python 包在 <project-root>/.venv 中使用了 poetry virtualenv 并以可编辑模式安装到 venv 中。

现在我想在集成的 VSCode 终端中调试命令行应用程序。为了能够做到这一点,我创建了一个 launch.json 配置,如下所示:

{
    // 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"
        }
    ]
}

如果我启动调试器,则会打开一个新终端,但会出现以下错误:

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>

似乎在执行命令行应用程序之前没有激活 venv,但之后才激活。根据 VSCode docs - Where the extension looks for environments and microsoft/vscode-python/issues/8372 poetry venvs 几乎不支持 ATM。

我已经尝试通过添加

来指定venv Python environment manually
{
    "python.pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe"
}

到项目本地.vscode/settings.json文件没有成功。

我该如何修复或解决这个问题?

这不是错误。默认的终端前缀应该是 PS \the current working directory\,所以当我们开始调试时,它会打开一个显示默认前缀的新终端实例。但是当我们查看它的执行脚本时:

第一个是.venv\python.exe我们select它作为解释器,调试完成后,启动环境。然后你再次选择调试,这个终端将保持.venv被激活。

事实证明,这个问题与 venv 管理无关,而是与必须如何处理 console_script 相关 w.r.t。调试: