close

[Solved] argparse: unrecognized arguments

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following argparse: unrecognized arguments error in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How argparse: unrecognized arguments Error Occurs?

Today I get the following argparse: unrecognized arguments error in python.

How To Solve argparse: unrecognized arguments Error ?

  1. How To Solve argparse: unrecognized arguments Error ?

    To Solve argparse: unrecognized arguments Error If you omit sys.argv then parse_args parses sys.argv as expected.

  2. argparse: unrecognized arguments

    To Solve argparse: unrecognized arguments Error If you omit sys.argv then parse_args parses sys.argv as expected.

Solution 1

Do not pass sys.argv as an argument to parse_args. Just use

option = parser.parse_args()

If you do pass sys.argv to parse_args, then the path or name of the script itself is the first item in sys.argv and thus becomes the value of option.filename. The hehe then becomes an unknown argument.

If you omit sys.argv then parse_args parses sys.argv as expected.

Solution 2

You can get around this by allowing unknown arguments

Replace

args = parser.parse_args()

with

args, unknown = parser.parse_known_args()

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read