VSCode的launch.json配置

 

11 人赞同了该文章

launch.json设置环境变量

在Vscode的运行调试中需要使用环境变量,可以使用env设置参数,如设置CUDA_VISIBLE_DEVICES变量

// launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {
                "CUDA_VISIBLE_DEVICES": "0"
            },
        }
    ]
}

launch.json定义中间变量

有时候配置了多个configurations,需要设置同一个参数,修改的时候全部都要改一次,这就很麻烦。 我们可以通过settings.json定义中间变量,实现自定义参数。 在.vscode文件夹下新建setting.json,再在launch.json中使用该变量。

// setting.json
{
    "CUDA_VISIBLE_DEVICES":"0"
}
// launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {
                "CUDA_VISIBLE_DEVICES": "${config:CUDA_VISIBLE_DEVICES}"
            },
        }
    ]
}

launch.json给python传参数

运行Python时,需要给*.py文件传参数,可以在args传入参数。

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "python": "${command:python.interpreterPath}",
            "name": "Python: train",
            "type": "python",
            "request": "launch",
            "program": "train.py",
            "console": "integratedTerminal",
            "env": {
                "CUDA_VISIBLE_DEVICES": "${config:CUDA_VISIBLE_DEVICES}"
            },
            "args": [
                "--config_file",
                "files/configs/x.yaml"
            ],
            "justMyCode": false
        },
    ]
}

本文使用 Zhihu On VSCode 创作并发布

编辑于 2023-10-19 21:51・广东

posted on 2026-09-16 14:28  漫思  阅读(5)  评论(0)    收藏  举报

导航