Skip to content

在 vscode 中用 debugpy 优雅的调试 python 代码

2024-03-23 · 508字 · 2分钟

以往在 vscode 中调试代码时,总需要把某个.sh文件中的配置照抄到launch.json中调试其中涉及的单个.py文件。即便有 chatgpt 的帮助,仍煞为繁琐。

譬如

sh
      # run feature and x-vectors extraction
      python VBx/predict.py \
          --in-file-list exp/list.txt \
          --in-lab-dir example/vad \
          --in-wav-dir example/audios/16k \
          --out-ark-fn exp/${filename}.ark \
          --out-seg-fn exp/${filename}.seg \
          --weights VBx/models/ResNet101_16kHz/nnet/final.onnx \
          --backend onnx

      # run variational bayes on top of x-vectors
      python VBx/vbhmm.py \
          --init AHC+VB \
          --out-rttm-dir exp \
          --xvec-ark-file exp/${filename}.ark \
          --segments-file exp/${filename}.seg \
          --xvec-transform VBx/models/ResNet101_16kHz/transform.h5 \
          --plda-file VBx/models/ResNet101_16kHz/plda \
          --threshold -0.015 \
          --lda-dim 128 \
          --Fa 0.3 \
          --Fb 17 \
          --loopP 0.99
json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug onnx predict",
      "type": "debugpy",
      "request": "launch",
      "program": "${workspaceFolder}/VBx/predict.py",
      "args": [
        "--in-file-list", "exp/list.txt",
        "--in-lab-dir", "example/vad",
        "--in-wav-dir", "example/audios/16k",
        "--out-ark-fn", "exp/ES2005a.ark",
        "--out-seg-fn", "exp/ES2005a.seg",
        "--weights", "VBx/models/ResNet101_16kHz/nnet/final.onnx",
        "--backend", "onnx"
    ],
      "console": "integratedTerminal",
      "stopOnEntry": false,
      "cwd": "${workspaceFolder}",
      "env": {},
      "showReturnValue": true
    },
    {
      "name": "Debug VBx inference",
      "type": "debugpy",
      "request": "launch",
      "program": "${workspaceFolder}/VBx/vbhmm.py",
      "args": [
        "--init","AHC+VB",
        "--out-rttm-dir","exp",
        "--xvec-ark-file","exp/ES2005a.ark",
        "--segments-file","exp/ES2005a.seg",
        "--xvec-transform","VBx/models/ResNet101_16kHz/transform.h5",
        "--plda-file","VBx/models/ResNet101_16kHz/plda",
        "--threshold","-0.015",
        "--lda-dim","128",
        "--Fa","0.3",
        "--Fb","17",
        "--loopP","0.99"
      ],
      "console": "integratedTerminal",
      "stopOnEntry": false,
      "cwd": "${workspaceFolder}",
      "env": {},
      "showReturnValue": true
    }
  ]
}

今天得知 一个相当优雅的办法,无须一一配置待传参数。只需要在.py文件前加上一个 debugpy 的监听器,同时在launch.json中加上一个调试器即可,注意监听端口与连接端口须一致。

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.1
    debugpy.listen(("localhost", 9501))
    print("Waiting for debugger attach")
    debugpy.wait_for_client()
except Exception as e:
    pass
json
    {
        "name": "Debug predict",
        "type": "debugpy",
        "request": "attach",
        "connect": {
            "host": "localhost",
            "port": 9501
        }
    },

这样,在仍旧运行原来的.sh文件,此时被调用的.py文件就会在端口等待连接调试器,然后点击对应端口的调试器就进入调试了!一切照旧,只是免去了配置冗长调试器的麻烦。

返回

人同此心,心同此理;如风沐面,若水润心