【问题标题】:Remote debugging with debugpy works from code but not from command line使用 debugpy 进行远程调试可以通过代码进行,但不能通过命令行进行
【发布时间】:2021-12-09 21:52:18
【问题描述】:

我有一个可以像这样运行的 python 二进制文件(使用 bazel 构建,但我认为这无关紧要):

$ bazel-bin/path/to/my_test

----------------------------------------------------------------------
Ran 5 tests in 0.228s

OK

我想使用debugpy 在 Visual Studio Code 中调试此二进制文件。我可以去我要调试的文件添加

import debugpy
debugpy.listen(('0.0.0.0', 5678))
debugpy.wait_for_client()

构建并运行二进制文件,它开始等待客户端,然后我使用这个 vscode 配置

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "host": "127.0.0.1",
            "port": 5678,
        }
    ]
}

一切正常。我可以下断点,跳代码,完美。

现在,如果不是在顶部添加代码,我会这样做:

python3 -m debugpy --listen 0.0.0.0:5678 --wait-for-client bazel-bin/path/to/my_test

看起来客户端正在等待连接。然后我开始在 vscode 上进行远程调试,然后……什么也没有发生。调试器在一秒钟内启动和完成。我没有看到任何日志,也没有错误。

我在哪里可以找到日志以查看客户端或 vscode 发生了什么?

【问题讨论】:

    标签: python debugging visual-studio-code remote-debugging


    【解决方案1】:

    你添加断点了吗?

    debugpy.breakpoint()

    import debugpy
    
    # 5678 is the default attach port in the VS Code debug configurations. Unless a host and port are specified, host defaults to 127.0.0.1
    debugpy.listen(5678)
    print("Waiting for debugger attach")
    debugpy.wait_for_client()
    debugpy.breakpoint() #must have
    print('break on this line')
    

    参考:https://code.visualstudio.com/docs/python/debugging

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 2021-12-14
      • 2015-01-19
      • 2011-12-08
      • 2013-09-19
      • 2021-02-19
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多