Vscode调试带参数的bash脚本
以bash ./shell/pretrain.sh 0 2023为例:
- 在Vscode中安装Bash Debug
- 配置编译器
点击之后生成/home/user/Demo/.vscode/launch.json文件,内容如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "bashdb",
"request": "launch",
"name": "Bash-Debug (simplest configuration)",
"program": "${file}"
}
]
}
- 修改文件内容的后两行,添加debug的文件目录和参数:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "bashdb",
"request": "launch",
"name": "Bash-Debug",
"program": "/home/user/Demo/shell/pretrain.sh",
"args": ["0", "2023"]
}
]
}
- 点击左上角的三角符号开始debug
- 运行之后报错如下:
- 为了解决以上问题,在launch.json中添加一行
"terminalKind": "integrated"
点击左上角的三角符号得到:
- 在终端可以切换其他虚拟环境,再重新点击运行即可在不同的虚拟环境中debug shell脚本文件。
以上步骤不能在脚本文件运行的python文件中debug。如果想debug脚本文件运行的python文件,可以尝试将脚本文件中的参数写入.vscode/launch.json。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python 调试程序: 当前文件",
"type": "debugpy",
"request": "launch",
"program": "/home/user/Demo/pretrain.py",
"console": "integratedTerminal",
"args": [
"--base_model", "/home/user/LLM/llama-7b-hf/",
"--data_path", "/home/user/Demo/data/movie/sequences.json",
"--output_dir", "/home/user/Demo/model/",
"--batch_size", "128",
"--micro_batch_size", "32",
"--num_epochs", "200",
"--learning_rate", "1e-4",
"--cutoff_len", "512",
"--lora_r", "8",
"--lora_alpha", "16",
"--lora_dropout", "0.05",
"--lora_target_modules", "[q_proj,v_proj]",
"--resume_from_checkpoint", "/home/user/Demo/alpaca-lora-7B",
"--sample", "64",
"--seed", "2023"
]
}
// {
// "type": "bashdb",
// "request": "launch",
// "name": "Bash-Debug",
// "program": "/home/user/Demo/shell/pretrain.sh",
// "terminalKind": "integrated",
// "args": [
// "0",
// "2023"
// ]
// }
]
}
通过这种方式就可以在python文件中打断点debug。
如果程序需要并行运行多个py文件,可以尝试使用Python自带的pdb模块,在代码中设置断点并进行调试。