Fixing Python SyntaxError: non-keyword after keyword arg

Python SyntaxError: non-keyword after keyword arg is an error which occurs when you put non-keyword arguments and keyword arguments in the wrong order.

In the blog post I attempt to explain why this error takes place and how you can solve it, I will also add other solutions that could solve the error if possible.

Exploring Python SyntaxError: non-keyword after keyword arg

This is an error which occurs when you put non-keyword arguments and keyword arguments in the wrong order.

The error can occur because of simple lines of code just like the examples bellow.

                                                                       #
inputFile = open((a), encoding = "ASCII", "n=True")
                                                                       #
                                                                       #
inputFile = open((a), encoding = "utf8", "r")
                                                                       #
                                                                       #
inputFile = open((a), name = "Yara", "present=True")
                                                                       #

Do not mix between errors. Make sure the error message looks like the error message bellow after double checking.

                                                                       #
SyntaxError: non-keyword arg after keyword arg
                                                                       #

In the section bellow we will explain the root of the error more and propose a possible fixe.

Solution : flip the position of the non-keyword args and the keyword args

Unlike other errors in python, this error is very clear, the error says non-keyword arg after keyword arg.

This means you have the arguments flipped, since in python non-keyword args should be positioned before normal keyword args.

This is how non-keyword args look like in python.

                                                                       #
date="02/09/2000", rank="1323", gender="male", age="30"  
                                                                       #

Normal keyword args or fixed parameters look like this

                                                                       #
present=False, active=True, open=True
                                                                       #

So if you do something like this

                                                                       #
inputFile = open((a), encoding = "utf8", "r")
                                                                       #

or this

                                                                       #
inputFile = open((a), encoding = "ASCII", "n=True")
                                                                       #

or this

                                                                       #
inputFile = open((a), name = "Yara", "present=True")
                                                                       #

You are making a big mistake. The solution is to flip the arguments and use this.

                                                                       #
inputFile = open((x), "r", encoding = "utf8")
                                                                       #

or this, for the second example

                                                                       #
inputFile = open((x), "n=True", encoding = "ASCII")
                                                                       #
                                                                       #
inputFile = open((a), "present=True", name = "Yara")
                                                                       #

The solution above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up

I hope you found a solution in our article, keep creating and keep coding, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/