Last updated: Apr 13, 2024
Reading time·4 min
The Python argparse "unrecognized arguments" error occurs for 3 main reasons:
sys.argv
list in the call to parser.parse_args()
.action
set to store_true
.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.
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.
# ⛔️ main.py: error: unrecognized arguments: example.txt python main.py example.txt --count 10
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()
.
# ⛔️ Should not pass sys.argv here args = parser.parse_args(sys.argv)
Instead, remove the argument and call the method without any arguments.
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.
python main.py example.txt --count 10
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.
# ✅ removed sys.argv args = parser.parse_args()
action
argument to store_true
for boolean argumentsIf you work with boolean arguments, set the action
argument to store_true
.
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}')
When the action
argument is set to store_true
:
--verbose
argument is not supplied, it gets set to False
.True
.This way you don't have to explicitly set a value for boolean arguments.
There is also a store_false
action, which means that:
True
if not supplied.False
if explicitly passed.parser.add_argument('-v', '--verbose', action='store_true')
Make sure you haven't:
parser.add_argument()
method.Suppose we have the following Python script.
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}')
If I now try to pass a Count
argument, I'd get the error.
# ⛔️ main.py: error: unrecognized arguments: --Count 10 python main.py 'example.txt' --Count 10
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.
parser.add_argument('-c', '--count')
So the following command works as expected.
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.
python your_script.py -h
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.
python main.py example.txt -c 100 -v
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.
parser.add_argument('-c', '--count') parser.add_argument('-v', '--verbose', action='store_true')
To solve the Python argparse "unrecognized arguments" error, make sure:
sys.argv
list in the call to parser.parse_args()
method.action="store_true"
.You can learn more about the related topics by checking out the following tutorials:
__main__
module in Path