SystemExit: 2 error when calling parse_args()

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.


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

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

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

parser.add_argument('-f')