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

如何在VS Code中调试由Bash脚本调用的Python预启动脚本?

问题

我想在VS Code里调试通过启动任务运行的Python文件:目前打开了script_01.py,已经设置好启动任务,会在调试运行script_01.py之前先跑script_02.py,现在需要script_02.py也能被调试。

当前相关配置文件如下:

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugging Test",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "justMyCode": true,
            "preLaunchTask": "startup",
        }
    ]
}

.vscode/tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label" : "startup",
            "type" : "shell",
            "command": "bash task_resources/startup.sh",
       },
    ]
}

task_resources/startup.sh

python3 script_02.py

我试过用debugpy,但没能搞定Bash脚本调用的Python脚本调试,想问这个需求能不能实现?


可行方案

当然可以实现,给你两种实用的解决方法:

方法1:直接把前置任务改成Python调试任务

不用通过shell脚本中转,直接让VS Code的调试器接管script_02.py的运行:

  1. 修改.vscode/tasks.json,把原来的shell任务换成Python调试任务:
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "startup",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/script_02.py",
            "justMyCode": false,
            "problemMatcher": []
        }
    ]
}
  1. 调整launch.json,确保前置任务执行完再启动主脚本调试:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugging Test",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "justMyCode": true,
            "preLaunchTask": "startup",
            "presentation": {
                "reveal": "always"
            }
        }
    ]
}

这样运行主调试任务时,VS Code会先启动script_02.py的调试会话,你可以正常设置断点,等script_02.py执行完,会自动启动script_01.py的调试。

方法2:用debugpy让script_02主动等待调试器连接

如果必须保留shell脚本的调用方式,就用debugpy的附加模式来实现:

  1. 先安装debugpy:
pip install debugpy
  1. 在script_02.py开头加上调试监听代码:
import debugpy

# 开启本地监听端口(端口号可以自己改,只要不冲突就行)
debugpy.listen(5678)
print("等待调试器连接...")
debugpy.wait_for_client()
print("调试器已连接!")

# 下面写你原来的script_02.py代码
  1. 修改startup.sh,让脚本后台运行(避免阻塞主调试任务启动):
python3 script_02.py &
  1. 在launch.json里添附加调试配置:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugging Test",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "justMyCode": true,
            "preLaunchTask": "startup",
            "preLaunchTaskWait": true
        },
        {
            "name": "Attach to script_02",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "justMyCode": false
        }
    ]
}

运行时先启动"Python Debugging Test"任务,此时script_02.py会打印等待连接的提示,然后手动启动"Attach to script_02"调试配置,就能给script_02.py断点调试了。等script_02.py执行完,主调试任务会自动启动script_01.py的调试。

如果希望script_02.py执行完再启动主脚本,可以去掉startup.sh里的&,但要记得及时手动附加调试器。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 19:07:51