I’m learning basics of Python and got already stuck at the beginning of argparse tutorial. I’m getting the following error:

import argparse
parser = argparse.ArgumentParser()
args = parser.parse_args()
usage: __main__.py [-h] echo
__main__.py: error: unrecognized arguments: -f
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

a %tb command gives the following output:

SystemExit                                Traceback (most recent call last)
<ipython-input-16-843cc484f12f> in <module>()
----> 1 args = parser.parse_args()

C:\Users\Haik\Anaconda2\lib\argparse.pyc in parse_args(self, args, namespace)
   1702         if argv:
   1703             msg = _('unrecognized arguments: %s')
-> 1704             self.error(msg % ' '.join(argv))
   1705         return args
   1706 

C:\Users\Haik\Anaconda2\lib\argparse.pyc in error(self, message)
   2372         """
   2373         self.print_usage(_sys.stderr)
-> 2374         self.exit(2, _('%s: error: %s\n') % (self.prog, message))

C:\Users\Haik\Anaconda2\lib\argparse.pyc in exit(self, status, message)
   2360         if message:
   2361             self._print_message(message, _sys.stderr)
-> 2362         _sys.exit(status)
   2363 
   2364     def error(self, message):

SystemExit: 2

How could I fix this problem?

argparse is a module designed to parse the arguments passed from the command line, so for example if you type the following at a command prompt:

$ python my_programme.py --arg1=5 --arg2=7

You can use argparse to interpret the --arg1=5 --arg2=7 part. If argparse thinks the arguments are invalid, it exits, which in general is done in python by calling sys.exit() which raises the SystemExit error, which is what you’re seeing.

So the problem is you’re trying to use argparse from an interactive interpreter (looks like ipython), and at this point the programme has already started, so the args should have already been parsed.

To try it properly create a separate python file such as my_programme.py and run it using python from a command line, as I illustrated.

[quick solution] Add an dummy parser argument in the code

parser.add_argument('-f')

had run into a similar issue. adding these lines fixed the issue for me.

import sys
sys.argv=['']
del sys

parse_args method, when it’s called without arguments, attempts to parse content of sys.argv. Your interpreter process had filled sys.argv with values that does not match with arguments supported by your parser instance, that’s why parsing fails.

Try printing sys.argv to check what arguments was passed to your interpreter process.

Try calling parser.parse_args(['my', 'list', 'of', 'strings']) to see how parser will work for interpreter launched with different cmdline arguments.

I know this question is nearly three years old but as dumb as it can sound, this exit error is also produced when you don’t have argparse installed instead of the default “This module can’t be found” error message. Just helping people that may have this error aswell.

Add an argument and assign some value works. I was passing args (ArgumentParser type object) from one function to another (not in a typical case, like, getting user args from terminal).

from argparse import ArgumentParser
parser = ArgumentParser()
# create and assign a dummy args
parser.add_argument('--myarg1')
args = parser.parse_args(['--myarg1', ''])

args.myarg2 = True  # actuals args assignment
myTargetFunction(args)  # passing args to another function  

I found without any actual args in parser, parse_args() gives error.