1

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?

4
  • 1
    That looks like it should work - what's the output when you print sys.argv? Commented Mar 31, 2022 at 3:28
  • @MiguelGuthridge Here is the output of print(sys.argv) ['c:/Users/lijianwei/Desktop/1/t.py'] Commented Mar 31, 2022 at 6:00
  • Ok yeah the program straight up isn't receiving arguments then Commented Mar 31, 2022 at 23:38
  • How are you launching the program? Commented Mar 31, 2022 at 23:38

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.