python代码无法从VSCode的launch.json文件获取 args参数

配置 launch.json 的参数为"args": [ "-s", "0","-g", "0",]时,在python代码中无法获取相应的值?

# python file
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
    '-s', '--seed',
    type=int,
    help = 'Random generator seed.',
    default=1,
)
parser.add_argument(
    '-g', '--gpu',
    type=int,
    help='CUDA GPU id (-1 for CPU).',
    default=-1,
)

args = parser.parse_args()
print(args)
print(args.seed)
print(args.gpu)
# launch.json
{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "args": [
                "-s", "0",
                "-g", "0",
            ]
        }
    ]
}

下面是终端的输出:

Namespace(seed=1, gpu=-1)
1
-1

配置好launch.json文件后,应该点运行和调试里面的运行按钮。而不是直接点文件右上角的debug。

img

你的代码也没有解析json文件的参数信息,只是通过parser.add_argument添加两个选项的默认值