【问题标题】:How can I debug Python console_script command line apps with the VSCode debugger?如何使用 VSCode 调试器调试 Python console_script 命令行应用程序?
【发布时间】:2021-02-09 21:01:05
【问题描述】:

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

setup.py:

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

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

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-pythondebugpy 没有成功。如何调试控制台脚本命令行应用程序(而不是包模块)?

【问题讨论】:

    标签: python visual-studio-code vscode-debugger


    【解决方案1】:

    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
    
    

    .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,则该解决方案不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      相关资源
      最近更新 更多