原始用法:(這里不使用)
配置launch.json,里面傳入參數然后debug,這里我們通常需要傳入的參數比較多,而且經常修改參數,直接去修改launch.json會比較麻煩,所以使用sh腳本比較方便。
{// 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. 安裝
- 安裝包
pip install debugpy -U
- 安裝vscode關于python的相關插件
2. 創建sh文件
文件內容如下:train.sh
python tools/train.py my_configs/ssdlite_mobilenetv2-scratch_8xb24-600e_coco.py
修改文件權限,變成可運行的文件。
chmod +x train.sh
3. 在python代碼里面(最前面加上這句話)
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.1debugpy.listen(("localhost", 9501))print("Waiting for debugger attach")debugpy.wait_for_client()
except Exception as e:pass
4. 在vscode的launch.json的configuration里面,加上這個配置
{"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腳本里面,然后就愉快的代碼調試。