VSCode优雅的使用debug

原始用法:(这里不使用)

配置launch.json,里面传入参数然后debug,这里我们通常需要传入的参数比较多,而且经常修改参数,直接去修改launch.json会比较麻烦,所以使用sh脚本比较方便。

bash 复制代码
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "image_demo.py",
            "type": "debugpy",
            "request": "launch",
            "program": "demo/image_demo.py",
            "console": "integratedTerminal",
            "justMyCode": false,
            "args": [
                "demo/demo.jpg",
                "my_configs/mobilenetv2_8xb24-ms-416-300e_coco.py",
                "--weight","checkpoints/yolov3_mobilenetv2_mstrain-416_300e_coco_20210718_010823-f68a07b3.pth",
                "--out-dir","outputs"  
            ]
        }
    ]
}

(优雅的用法)直接使用sh文件命令运行debug

1. 安装

  1. 安装包 pip install debugpy -U
  2. 安装vscode关于python的相关插件

2. 创建sh文件

文件内容如下:train.sh

bash 复制代码
python tools/train.py my_configs/ssdlite_mobilenetv2-scratch_8xb24-600e_coco.py

修改文件权限,变成可运行的文件。

bash 复制代码
chmod +x train.sh

3. 在python代码里面(最前面加上这句话)

bash 复制代码
import debugpy
try:
    # 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(("localhost", 9501))
    print("Waiting for debugger attach")
    debugpy.wait_for_client()
except Exception as e:
    pass

4. 在vscode的launch.json的configuration里面,加上这个配置

bash 复制代码
{
            "name": "sh_file_debug",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 9501
            }
        },

🚨 上面的端口号都写一样。别搞错了。

5. 运行sh脚本

例如 ./train.sh

这里我们会看到Waiting for debugger attach,这时候我点击下面图片中左上角的运行按钮,对应我们上面"name": "sh_file_debug",这个的名称,这样就进入Debug,我们要传入的参数都在sh脚本里面,然后就愉快的代码调试。

相关推荐
Learn Beyond Limits17 分钟前
Transfer Learning|迁移学习
人工智能·python·深度学习·神经网络·机器学习·ai·吴恩达
love530love2 小时前
【保姆级教程】阿里 Wan2.1-T2V-14B 模型本地部署全流程:从环境配置到视频生成(附避坑指南)
人工智能·windows·python·开源·大模型·github·音视频
He1955012 小时前
Go初级之十:错误处理与程序健壮性
开发语言·python·golang
和鲸社区3 小时前
《斯坦福CS336》作业1开源,从0手搓大模型|代码复现+免环境配置
人工智能·python·深度学习·计算机视觉·语言模型·自然语言处理·nlp
豌豆花下猫4 小时前
Python 潮流周刊#118:Python 异步为何不够流行?(摘要)
后端·python·ai
THMAIL4 小时前
深度学习从入门到精通 - LSTM与GRU深度剖析:破解长序列记忆遗忘困境
人工智能·python·深度学习·算法·机器学习·逻辑回归·lstm
携欢4 小时前
CodeQL(Mac)安装与测试(Visual Studio)简明指南
ide·vscode·macos
wheeldown4 小时前
【数学建模】数据预处理入门:从理论到动手操作
python·数学建模·matlab·python3.11
多打代码5 小时前
2025.09.05 用队列实现栈 & 有效的括号 & 删除字符串中的所有相邻重复项
python·算法
@CLoudbays_Martin115 小时前
为什么动态视频业务内容不可以被CDN静态缓存?
java·运维·服务器·javascript·网络·python·php