如何用VS Code调试外部应用启动的Python程序?以GuildAI为例
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:
- Open the Run and Debug panel in VS Code (
Ctrl+Shift+D), click "create a launch.json file", and select your Python environment. - 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 theguildexecutable (ensure it's in your PATH, or use the full path if needed).justMyCode: Set tofalseto ensure the debugger doesn't skip yourtrain.pycode (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:
- 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"
- Forward the remote port to your local machine using SSH:
ssh -L 5678:localhost:5678 your_username@remote_server_ip
- 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
debugpyis installed in the Python environment Guild uses (runpip install debugpyin 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

