Python Argparse Error: Error: Argument Count: Invalid Int Value
I am trying to run below code in jupyter notebook. import argparse parser = argparse.ArgumentParser(description='Example with non-optional arguments') parser.add_argument('count',
Solution 1:
Normally a script like that is run as a stand alone file, e.g.
python foo.py 23 inches
The shell converts '23 inches' in a list of strings, which is available as sys.argv[1:]
inside the program.
parser.parse_args()
uses this sys.argv[1:]
as the default. But when run from within a Ipython session or Notebook, that list has the values that initialized the session. That file name given as an invalid integer value was part of that initialization, and isn't usable by your parser
.
To test this script you need to provide a relevant list of strings, e.g.
parser.parse_args(['23', 'lbs'])
Or import sys
and modify sys.argv
as described in one of the linked answers.
https://docs.python.org/3/library/argparse.html#beyond-sys-argv
Post a Comment for "Python Argparse Error: Error: Argument Count: Invalid Int Value"