栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

利用vscode调试:launch.json

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用vscode调试:launch.json

demo:
  1. 默认:
    一般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"
        }
    ]
}
  1. 添加路径、环境
    可以额外添加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",
        }
    ]
}
  1. 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的解释:

参考
  • vscode官方教程
  • 使用vscode进行远程服务器代码调试
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/344189.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号