如何使用 VSCode 调试器调试 Python console_script 命令行应用程序?

thi*_*ybk 7 python visual-studio-code vscode-debugger

我有一个 Python 包package_name,它提供了一个命令行应用command-line-app-name程序console_script

setup.py:

setup(
    ...
    entry_points={"console_scripts": ["command-line-app-name=package_name.cli:main"]},
    ...
)
Run Code Online (Sandbox Code Playgroud)

virtualenv 位于<project>/.venv并由pipenv. pipenv托管 venvs 应支持 VSCode 调试集成。我创建了一个调试器配置launch.json文件,并将 Python 路径设置为 venv ( pythonPath):

{
    // 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: command-line-app-name",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "program": "command-line-app-name",
            "linux": {
                "pythonPath": "${workspaceFolder}/.venv/bin/python",
                "args": ["-r", "/home/florian/gitlab/package_name/data/Test_MRM.d"]
            },
            "windows": {
                "pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe",
                "args": ["-r", "D:\\MassHunter\\Data\\demo_0000.d"],
            },
            "console": "integratedTerminal"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

Windows 和 Linux 特定的 venv python 可执行文件和命令行参数不应产生影响。如果我运行调试器,我会得到:FileNotFoundError: [Errno 2] No such file or directory: '/home/florian/gitlab/package-name/command-line-app-name'。看来我在某种程度上误解了文档。我试图找到关于vscode-pythondebugpy的帮助,但没有成功。如何调试控制台脚本命令行应用程序(而不是包模块)?

thi*_*ybk 7

console_scripts无法开箱即用地进行调试。解决方案是直接调用入口点函数("program": "${workspaceRoot}/package_name/cli.py",)。if __name__ == '__main__':这就需要在相应的模块中添加惯用语(这里cli.py:)。就我而言,使用的命令行参数解析器是click. 然而,其他命令行解析器库的伪代码应该非常相似。

package_name/cli.py:

@click.command()
@click.option(...)
def main(<args>, <kwargs>):
    ...


if __name__ == '__main__':
    main()  # pylint: disable=no-value-for-parameter

Run Code Online (Sandbox Code Playgroud)

.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: command-line-app-name",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "program": "${workspaceRoot}/package_name/cli.py",
            "linux": {
                "pythonPath": "${workspaceFolder}/.venv/bin/python",
                "args": ["-r", "/home/florian/gitlab/package_name/data/Test_MRM.d"]
            },
            "windows": {
                "pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe",
                "args": ["-r", "D:\\MassHunter\\Data\\demo_0000.d"],
            },
            "console": "integratedTerminal"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

注意:用于管理 venv 的工具会有所不同。如果 venv 使用 进行管理,此解决方案确实有效pipenv。如果使用 管理 venv,则该解决方案不起作用poetry