利用vscode调试:launch.json

本文介绍了如何在VSCode中配置launch.json文件,以调试Python程序。内容包括设置当前文件调试、指定工作目录、环境变量以及传递参数给脚本。同时强调了launch.json的正确性和兼容性,提醒用户在调试前检查配置的语法错误,并提供了官方文档链接以获取更多帮助。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 添加路径、环境
    可以额外添加cwdenv来指定需要调试的文件路径、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的解释:
在这里插入图片描述

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值