如何使用 VSCode 调试器调试 Python console_script 命令行应用程序? [英] How can I debug Python console_script command line apps with the VSCode debugger?

查看:82
本文介绍了如何使用 VSCode 调试器调试 Python console_script 命令行应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a Python package package_name which provides a command line application command-line-app-name as console_script:

setup.py:

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

virtualenv 位于 /.venv 并使用 pipenv 进行管理.pipenv 托管的 venv 应该支持 VSCode 调试集成.我创建了一个调试器配置 launch.json 文件,其中设置了 venv 的 Python 路径(pythonPath):

The virtualenv is located in <project>/.venv and managed with pipenv. pipenv managed venvs should support VSCode debugging integration. I've created a debugger configuration launch.json file with setting the Python path to the 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"
        }
    ]
}

Windows 和 Linux 特定的 venv python 可执行文件和命令行参数不应该有影响.如果我运行调试器,我会得到:FileNotFoundError: [Errno 2] No such file or directory: '/home/florian/gitlab/package-name/command-line-app-name'.似乎我以某种方式误解了文档.我试图寻求帮助 w.r.t.vscode-python 以及 debugpy 没有成功.如何调试控制台脚本命令行应用程序(而不是包模块)?

The Windows and Linux specific venv python executable and command line arguments should not have an impact. If I run the debugger I get: FileNotFoundError: [Errno 2] No such file or directory: '/home/florian/gitlab/package-name/command-line-app-name'. It seems like I'm miss-interpreting the documentation somehow. I tried to find help w.r.t. vscode-python as well as debugpy without success. How can I debug a console script command line app (instead of a package module)?

推荐答案

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

console_scripts cannot be debugged out-of-the-box. The solution is to call the entry point function directly instead ("program": "${workspaceRoot}/package_name/cli.py",). This requires to add the if __name__ == '__main__': idiom in the corresponding module (here: cli.py). In my case the command line argument parser used is click. However the pseudo-code should be very similar for other command line parser libs.

package_name/cli.py:

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


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

.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"
        }
    ]
}

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

NOTE: The tool used to manage the venv makes a difference. This solution does work in case the venv is managed with pipenv. The solution does not work in case the venv is managed with poetry.

这篇关于如何使用 VSCode 调试器调试 Python console_script 命令行应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆