2

I have a code that takes the command-line arguments into a parser and modifies some configuration settings. Something like this:

command:

python mycode.py --config-file "some_file.yaml" SOMETHING.subsetting_a 2 SOMETHING.subsetting_b 3

and then it does:

import argparse
parser = argparse.ArgumentParser(description="Some description here")
parser.add_argument(
    "--config-file",
    default="",
    metavar="FILE",
    help="path to config file",
    type=str,
)
//some more 'add_argument' lines here
args = parser.parse_args()

But as I am using jupyter notebook, it would be easier to provide the arguments directly to the parser, as if they come from the command-line. How can I create a string containing the command (as mentioned above) and pass it to parser?

1
  • 1
    You can also create a args = argparse.Namespace(config_file='some_file.yaml', ...) (skipping the parse_args step). Commented May 30, 2019 at 6:30

2 Answers 2

3

parse_args's first optional argument is the list of arguments to parse, the signature is:

ArgumentParser.parse_args(args=None, namespace=None)

It just takes args from sys.argv if you don't provide it.

So just call it as:

args = parser.parse_args(['mycode.py', '--config-file', "some_file.yaml", 'SOMETHING.subsetting_a', '2', 'SOMETHING.subsetting_a'])

(with the list containing whatever you like instead) and it will use it instead of sys.argv.

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

Comments

1

Note: As @ShadowRanger mentioned, there is no need to use sys.argv. See his response.


One way is to use sys.argv to mimic the command-line arguments:

import sys

sys.argv = [
    "--config-file" , "some_file.yaml",
    "SOMETHING.subsetting_a" , "2",
    "SOMETHING.subsetting_b" , "3"]

args = parser.parse_args(sys.argv)

The content of args is something liek this:

> Namespace(config_file='some_file.yaml', opts=['SOMETHING.subsetting_a', '2', 'SOMETHING.subsetting_b', '3')

which is similar to the output of print(parser.parse_args()).

5 Comments

You passed non-existent arguments to parse_args. I think you may have misunderstood the documentation; you could have passed the list directly as an argument to parse_args instead of modifying sys.argv at all.
That was a typo. Thanks. And yes you are right about not needing sys.argv. I will update it.
The unit test file for argparse tests both a direct list of strings and a doctored sys.argv.
@hpaulj: Yar. It's perfectly legal, the reason not to do it is that you shouldn't be using global state to solve a local problem. The contents of sys.argv often mean something, and you shouldn't mess with them willy-nilly if there is any other way to accomplish your aims.
It this case of a jupyter notebook the real sys,argv has already been used by the jupyter server. So modifying it for 'local' use should be fine - though it may affect other notebooks on the same server.

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.