You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用VS Code调试外部应用启动的Python程序?以GuildAI为例

Debugging GuildAI-Wrapped Scripts in VS Code (Local & Remote)

Absolutely, you can debug train.py when it's wrapped and executed via guild run train.py in VS Code—both locally and remotely. Here are the most reliable approaches to make this work:

1. Let VS Code Launch guild run Directly & Debug

You can configure VS Code's debug launch settings to invoke the guild command directly, attaching the debugger to the wrapped Python process. Here's how:

  1. Open the Run and Debug panel in VS Code (Ctrl+Shift+D), click "create a launch.json file", and select your Python environment.
  2. Replace the default config with this custom one:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "GuildAI: Train Script",
            "type": "python",
            "request": "launch",
            "command": "guild",
            "args": ["run", "train.py"],
            "justMyCode": false,
            "console": "integratedTerminal"
        }
    ]
}
  • command: Points to the guild executable (ensure it's in your PATH, or use the full path if needed).
  • justMyCode: Set to false to ensure the debugger doesn't skip your train.py code (Guild's wrapper code is treated as "external", but we want to focus on your script).
  • console: Uses the integrated terminal so you can see Guild's output alongside debug logs.

Just set your breakpoints in train.py, then run this debug configuration—VS Code will handle launching Guild and stopping at your breakpoints.

2. Attach to Guild's Wrapped Python Process (More Reliable for Subprocesses)

Sometimes Guild spawns a separate Python subprocess, which the direct launch method might miss. In this case, force Guild to start Python with debugpy (VS Code's debugging library) preloaded:

Step 1: Prepare the VS Code Attach Config

First, add a standard "Attach" configuration to your launch.json:

{
    "name": "Python: Attach",
    "type": "python",
    "request": "attach",
    "connect": {
        "host": "localhost",
        "port": 5678
    },
    "justMyCode": false
}

Step 2: Run Guild with Debug Flags

Execute Guild in your terminal, passing debugpy arguments to the underlying Python process:

guild run train.py --python-flags "-m debugpy --listen 0.0.0.0:5678 --wait-for-client"
  • --python-flags: Passes arguments directly to the Python interpreter Guild uses.
  • --wait-for-client: Makes the script pause until VS Code's debugger connects, so you don't miss early breakpoints.

Step 3: Start the Debugger in VS Code

Click the "Play" button next to the "Python: Attach" configuration in the Run panel. The debugger will connect to Guild's Python process, and your breakpoints will trigger as expected.

3. Remote Debugging with GuildAI

If you're running Guild on a remote server, the process is similar—you just need to handle port forwarding:

  1. On the remote server, run Guild with debugpy listening on all interfaces:
guild run train.py --python-flags "-m debugpy --listen 0.0.0.0:5678 --wait-for-client"
  1. Forward the remote port to your local machine using SSH:
ssh -L 5678:localhost:5678 your_username@remote_server_ip
  1. In your local VS Code, use the same "Python: Attach" configuration from Step 2 above. The debugger will connect to the forwarded port, letting you debug the remote script as if it were local.

Key Notes

  • Ensure debugpy is installed in the Python environment Guild uses (run pip install debugpy in that environment).
  • Double-check that VS Code is using the same Python interpreter as Guild (use the interpreter selector in the bottom-left corner to confirm).
  • If you're using a virtual environment for Guild, make sure it's activated before running debug configurations or terminal commands.

内容的提问来源于stack exchange,提问作者DsCpp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:55:30