Python argparse Unrecognized Arguments

Python argparse is a parser for command line arguments and options. It allows the user of a program to pass arguments into a Python script from the command line.

This article will cover two things: a quick example of how argparse is used and why we get the “Unrecognized Arguments” error (and how to solve it).

We will use the work_through_args.py script with the following contents to demonstrate concepts.

import argparse
# Initialize the parser
parser = argparse.ArgumentParser()
# Arguments.
# positional argument -always required
parser.add_argument('value1', type=float)
# Another argument - set as required 	 
parser.add_argument('-v2', "--value2", required = True, type=float)
# Another argument - by default, it is the optional
parser.add_argument('-v3', '--value3', type=float)
# True or False argument. It stores True in this case.
parser.add_argument('-v4', '--value4', action='store_true')
# Print the arguments
args = parser.parse_args()
print(args)
# Addtion of value
print(f"{args.value1}+{args.value2}=", args.value1+args.value2)

The script accepts three arguments – a positional argument, -v2/–value2, -v3/–value3, and -v4/–value4.

If we want to execute the above script from the command line, we need to run a command line this

python3 </path/to/work_through_args.py> <positional> -v2 <value> -v3 <value> -v4 <value>

See examples in the Figure below.

What Causes “Unrecognized Arguments” Error

This error is raised when we pass arguments that argparse does not expect. Here are some cases when this happens,

Case 1: Issuing argument that is not expected by argparse

The script above expected the arguments -v2/–value2, -v3/–value3 and -v4/–value4 NOT –value5. That is why the error was raised.

Case 2: Passing a value to an argument that does not expect the value

Some arguments do not expect the value to be passed to them. For example, -v4 in the above script does not expect any value to be supplied, and if we do, we end with an “Unrecognized Arguments” Error.

The argument -v4 already stores a value (True, if supplied; otherwise, False). That means -v4 does not accept any value, and attempts to give 56 as a value ended in error.

Solving “Unrecognized Arguments” Error

Solution 1: Verify that you are passing valid arguments

For Case 1 above – you need to pass arguments expected by argparse.

For Case 2 above – do not pass values to arguments that do not accept them.

Solution 2: Using the parser.parse_known_args() instead of parser.parse_args()

Unlike the parser.parse_args() function, parser.parse_known_args() accepts extra arguments. Here is an excerpt about it from the argparse documentation.

“Sometimes a script may only parse a few of the command-line arguments, passing the remaining arguments on to another script or program. In these cases, the parse_known_args() method can be useful. It works much like parse_args() except that it does not produce an error when extra arguments are present. Instead, it returns a two-item tuple containing the populated namespace and the list of remaining argument strings.” – Source: argparse documentation.

To use parse_known_args() in our work_through_args.py script replace the lines

args = parser.parse_args()
print(args)

with

args, unknown_args = parser.parse_known_args()
print(args, unknown_args)

Then we can run our script with extra arguments.

In the last two executions in the Figure above, we have supplied extra arguments (and values), but the “Unrecognized Arguments” Error was not raised.

Conclusion

“Unrecognized Arguments” Error in argparse is raised when we supply arguments not expected by argparse. To solve the error, verify that you pass valid arguments. If you must allow extra arguments, use parse_known_args() instead of parse_args().