demo:
- 默认:
一般launch.json会直接帮你生成如下json文件,默认调试当前打开的python,没有设置任何参数:
{
// 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": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
- 添加路径、环境
可以额外添加cwd
和env
来指定需要调试的文件路径、GPU环境,name
字段可以自定义该config的名字(用以在debug键中区分不同settup)
{
// 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": [
{ "cwd": "/home/XXX/YYY/ZZZ/",
"env": {"CUDA_VISIBLE_DEVICES":"3"},
"name": "Python: train_QA",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
}
]
}
- args
需要传给对应python文件的参数可以用args
指定
{
// 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": [
{ "cwd": "/home/XXX/YYY/ZZZ/",
"env": {"CUDA_VISIBLE_DEVICES":"0"},
"name": "Python: lrz eval",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": [
"--dataset","cityscapes",
"--filter-scale","1",
"--random-mirror",
"--random-scale",
"--update-mean-var",
"--train-beta-gamma"
]
}
]
}
当然,launch.json里面可以定义多个文件的分别的运行参数,因为每个文件的运行参数是一个dict,只要把每个dict都append到launch.json的"configurations"字段的list中就行。如下,debug时可以自己选择confg(config的名字就是上面设置的name
):
tips
根据官方文档建议,不同的debugger的attributes都是有点不一样的,比方说node.js就不能用来跑python。同样,每种attributes的字段值合法性也需要考虑,若是有某些字段不符合要求,运行时会报错:
比方说,env期望dict的value都是string,如上图编译器会报错,这个时候强制运行的话:
所以debug之前记得确保launch.json没有语法错误,如果不知道python debugger有哪些attributes以及对应的语法要求,最好提前看一下help。或者把鼠标放在对应attribute上面,会有每种attributes的解释: