0
votes
import argparse

parser = argparse.ArgumentParser(description='Demo')
parser.add_argument('--verbose',
    action='store_true',
    help='verbose flag' )

args = parser.parse_args()

if args.verbose:
   print("~ Verbose!")
else:
   print("~ Not so verbose")

I'm getting this error

usage: ipykernel_launcher.py [-h] [--verbose] ipykernel_launcher.py: error: unrecognized arguments: -f C:\Users\Sourav\AppData\Roaming\jupyter\runtime\kernel-c07f728a-968e-40af-b09e-e7d9b67b47fe.json

SystemExit: 2

C:\Users\Sourav\AppData\Local\conda\conda\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py:2855: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

1
How are you trying to run this script? Looks like a Jupyter notebook? In any case the sys.argv list that the parser sees includes this '-f'` argument, which it is not prepared to handle.hpaulj
@hpaulj what should I change then?SrGrace
I don't know what you want to do! To test/learn argparse don't use a notebook. Run it as an ordinary script from shell. If you want to set some value in a notebook, don't use argparse.hpaulj
I tried it using shell too, but couldn't do it.SrGrace
Actually, I'm just learning how to use argparse module. Any help would be useful for me.SrGrace

1 Answers

0
votes

In a shell window (linux bash):

1317:~/mypy$ python stack52235399.py 
~ Not so verbose
0940:~/mypy$ python stack52235399.py --verbose
~ Verbose!
0940:~/mypy$ cat stack52235399.py 
import argparse

parser = argparse.ArgumentParser(description='Demo')
parser.add_argument('--verbose',
    action='store_true',
    help='verbose flag' )

args = parser.parse_args()

if args.verbose:
   print("~ Verbose!")
else:
   print("~ Not so verbose")