Python argparse: unrecognized arguments error [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
4 min

banner

# Python argparse: unrecognized arguments error [Solved]

The Python argparse "unrecognized arguments" error occurs for 3 main reasons:

  1. Passing the sys.argv list in the call to parser.parse_args().
  2. Forgetting to supply a value for a boolean argument that is not initialized with action set to store_true.
  3. Supplying misspelled or incorrect arguments when running the Python script.

# Passing the sys.argv list in the call to parser.parse_args()

The most common cause of the error is passing the sys.argv list in the call to ArgumentParser.parse_args.

Here is an example of how the error occurs.

main.py
import argparse import sys parser = argparse.ArgumentParser( description='A sample Argument Parser.' ) parser.add_argument('filename') parser.add_argument('-c', '--count') parser.add_argument('-v', '--verbose', action='store_true') # ⛔️ Should not pass sys.argv here args = parser.parse_args(sys.argv) print(f'args.filename {args.filename}') print(f'args.count {args.count}') print(f'args.verbose {args.verbose}')

The main.py file has a positional filename argument and --count and --verbose arguments.

Suppose, I run the following command.

shell
# ⛔️ main.py: error: unrecognized arguments: example.txt python main.py example.txt --count 10

argparse error unrecognized arguments

I passed the example.txt value for the filename argument and set the --count argument to 10.

Everything should work as expected, however, it does not because we passed sys.argv in the call to parser.parse_args().

main.py
# ⛔️ Should not pass sys.argv here args = parser.parse_args(sys.argv)

Instead, remove the argument and call the method without any arguments.

main.py
import argparse parser = argparse.ArgumentParser( description='A sample Argument Parser.' ) parser.add_argument('filename') parser.add_argument('-c', '--count') parser.add_argument('-v', '--verbose', action='store_true') # ✅ removed sys.argv args = parser.parse_args() print(f'args.filename {args.filename}') print(f'args.count {args.count}') print(f'args.verbose {args.verbose}')

Now, I'll rerun the same command.

shell
python main.py example.txt --count 10

remove sys argv argument

The code for this article is available on GitHub

If you pass sys.argv to parse_args() the name of the Python script is the first item in the sys.argv list.

Therefore, it gets supplied as the filename positional argument and the value we provided (example.txt) is an unrecognized argument.

Once you remove sys.argv from the parse_args() method call the error should be resolved.

main.py
# ✅ removed sys.argv args = parser.parse_args()

# Make sure to set the action argument to store_true for boolean arguments

If you work with boolean arguments, set the action argument to store_true.

main.py
import argparse parser = argparse.ArgumentParser( description='A sample Argument Parser.' ) parser.add_argument('filename') parser.add_argument('-c', '--count') # 👇️ set action to `store_true` parser.add_argument('-v', '--verbose', action='store_true') args = parser.parse_args() print(f'args.filename {args.filename}') print(f'args.count {args.count}') print(f'args.verbose {args.verbose}')
The code for this article is available on GitHub

When the action argument is set to store_true:

  1. If the --verbose argument is not supplied, it gets set to False.

verbose argument not supplied

  1. If it is supplied without a value, it gets set to True.

verbose argument is supplied

This way you don't have to explicitly set a value for boolean arguments.

There is also a store_false action, which means that:

  1. The argument is set to True if not supplied.
  2. The argument is set to False if explicitly passed.
main.py
parser.add_argument('-v', '--verbose', action='store_true')

# Make sure you haven't misspelled arguments or supplied unexpected arguments

Make sure you haven't:

  1. Misspelled any of the arguments as argument names are case-sensitive.
  2. Supplied arguments that you haven't added via the parser.add_argument() method.

Suppose we have the following Python script.

main.py
import argparse parser = argparse.ArgumentParser( description='A sample Argument Parser.' ) parser.add_argument('filename') parser.add_argument('-c', '--count') parser.add_argument('-v', '--verbose', action='store_true') args = parser.parse_args() print(f'args.filename {args.filename}') print(f'args.count {args.count}') print(f'args.verbose {args.verbose}')
The code for this article is available on GitHub

If I now try to pass a Count argument, I'd get the error.

shell
# ⛔️ main.py: error: unrecognized arguments: --Count 10 python main.py 'example.txt' --Count 10

make sure arguments are not misspelled

The error is caused because we didn't define a --Count argument in our Python script.

The argument we defined is spelled in all lowercase letters.

main.py
parser.add_argument('-c', '--count')

So the following command works as expected.

shell
python main.py 'example.txt' --count 10

You should also make sure that you aren't supplying any arguments that you haven't defined in your Python script via the parser.add_argument() method.

The easiest way to do this is with the python your_script.py -h command.

shell
python your_script.py -h

issue h command

As shown in the screenshot, the -h or --help option prints a helpful message that describes what the Python program does.

The message shows that the program has a filename position argument.

It also has options:

  • -c or --count
  • -v or --verbose

If you try to supply any other arguments when calling the program, you'll get the "unrecognized arguments" error.

You can also use the shorthand argument names.

shell
python main.py example.txt -c 100 -v

using shorthand argument names

We supplied a value for the -c (--count) argument but did not supply one for the -v (--verbose) argument because it is a boolean.

This is how we defined the two arguments in our Python script.

main.py
parser.add_argument('-c', '--count') parser.add_argument('-v', '--verbose', action='store_true')

# Conclusion

To solve the Python argparse "unrecognized arguments" error, make sure:

  1. You haven't passed the sys.argv list in the call to parser.parse_args() method.
  2. You haven't forgotten to supply a value for a boolean argument that is not set with action="store_true".
  3. You haven't misspelled argument names when running your Python program.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.