gpt4 book ai didi

python - 如何在使用 bazel 构建的项目中使用 vscode python 调试器?

转载 作者:行者123 更新时间:2023-12-04 12:25:01 25 4
gpt4 key购买 nike

我想调试一个 python 文件,它有一些只出现在 runfiles 中的依赖项从巴泽尔。如何使用 vscode 调试器调试 bazel 构建?

最佳答案

正如某位名人所说,“是的,我们可以”。

您需要使用“ptvsd”python 包。

一次性设置

  • 在 Bazel 中添加“ptvsd”作为 Python 依赖项
  • 在 VS Code 中,在您的 launch.json 中文件,添加如下配置:
  • {
    "name": "Python: Attach",
    "type": "python",
    "request": "attach",
    "port": 5724,
    "host": "localhost"
    },

    调试

    当您要调试特定文件时:
  • 在要调试的 Python 文件中,添加以下几行:

  • import ptvsd
    ptvsd.enable_attach(address=('localhost', 5724), redirect_output=True)
    print('Now is a good time to attach your debugger: Run: Python: Attach')
    ptvsd.wait_for_attach()
  • 像往常一样在这个文件上运行 Bazel(例如 bazel run :server)
  • 执行将停止在“现在是附加调试器的好时机:运行:Python:附加”
  • 在 VS Code 中,点击我们之前设置的“Python: Attach”调试选项:

  • Python attach button
  • 就是这样!

  • 随意更改端口,在本例中为 5724。

    关于python - 如何在使用 bazel 构建的项目中使用 vscode python 调试器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59334688/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com