pythonvscode-debuggerargs

python program can not get the args from the launch.json of VSCode?


when I set the launch.json with "args": [ "-s", "0","-g", "0",], I do not get the values as I want in the python program.

# 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",
            ]
        }
    ]
}

Here are the output in the terminal.

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

Now what I can do is modify the default value in the python program.But why can't I pass the args from launch.json? How can I solve this problem?


Solution

  • As the image shows, You should click the button "run" in the "run and debug" page(on the left side), Not click the run button on the top right of the page.enter image description here