?
问题描述
使用vscode可以很方便地添加断点,进行代码调试。
在使用服务器时,我们的python代码通常是通过bash脚本来执行的,那么如何进行debug呢?
待运行的bash 脚本示例
前半段定义了一些参数,后半段是执行python代码
1 2 3 4 5 6 7 8 9 10 11 12
| export CUDA_VISIBLE_DEVICES=1,2 model_path=/models/Mistral-7B-Instruct-v0.2/ output_path=/project/datagen/evaluation/mmlu data_path=/mmlu_data
python -m eval_mmlu.py \ --model ${model_path} \ --data_dir ${data_path} \ --save_dir ${output_path} \ --ntrain 5 \ --subject elementary_mathematics \
|
如果我们想要运行bash脚本的同时进行调试python代码,很可惜,右上角并没有类似调试python文件时的虫虫debug符号

解决方式
第零步,下载debugpy
第一步,修改launch.json文件
输入ctrl + p, 查找launch.json文件,用于配置调试设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| { // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python 调试程序: 当前文件", "type": "debugpy", "request": "attach", "connect": { "host": "localhost", "port": 8890 } } ] }
|
我们在本地的8890端口上开放了调试连接。使用这个配置,VS Code的调试器将尝试连接到这个运行中的程序,允许你进行调试
第二步,修改bash脚本
在执行python文件前加上
-m debugpy –listen localhost:8890 –wait-for-client \
完整代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13
| export CUDA_VISIBLE_DEVICES=1,2 model_path=/models/Mistral-7B-Instruct-v0.2/ output_path=/project/datagen/evaluation/mmlu data_path=/mmlu_data
python -m debugpy --listen localhost:8890 --wait-for-client \ eval_mmlu.py \ --model ${model_path} \ --data_dir ${data_path} \ --save_dir ${output_path} \ --ntrain 5 \ --subject elementary_mathematics \
|
通过使用debugpy并在8890端口上监听,这个脚本允许VS Code连接并进行远程调试,这与之前提到的launch.json配置相匹配。