我有一个 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 发生了什么?
你添加断点了吗?
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')
您可以在附加到调试服务器之前在Python源文件中放置一个断点,然后它会在附加后命中并断回VSCode。