Solving Python SyntaxError: invalid syntax with async def ( sometimes happens with discord.py )

Python SyntaxError: invalid syntax with async def ( sometimes happens with discord.py ) is an error which occurs because of compatibility issues between discord.py and python and sometimes the same error occurs for other reasons. 

In this blog post I attempt to present a clear explanation of why this error takes place and how you to solve it, I am also going to explain other ways to solve the error if possible.

Exploring Python SyntaxError: invalid syntax with async def ( sometimes happens with discord.py )

This is an error which occurs because of compatibility issues between discord.py and python and sometimes the same error occurs for other reasons. 

Beware of mixing between errors. Double check if the error message looks like the error message bellow, then continue.

                                                                       #
SyntaxError: invalid syntax
                                                                       #

Bellow are a couple of methods that I have tried and that have successfully removed the error for me.

Solution 1 : Rename async.py to a different name inside the site-packages folder

Async is a reserved keyword in some version of python which might be why you are getting the error.

The solution is to navigate to the site-packages python location.

Mine is located in the location bellow, It depends on your individual case, this folder can be found in a different place/path.

                                                                       #
/home/peter/.local/lib/python3.9/site-packages
                                                                       #

Then Rename async.py to a different name. asyncA.py sounds like a great name.

Then run your code as you did before, you can try the fix bellow if this method did not work for you.

Solution 2 : Upgrade to a new version of Python supported by discord.py

If you are running an older python version, like python 3.4 or python 3.6 you should understand that these versions are not supported by discord.py.

You should try to install the latest version of PythonPython 3.9 since its the one that seems to work fine with discord.py at least in my personal experience.

When installing python, its paramount to add Python to PATH since forgetting this can cause all sorts of errors, even SSL errors with discord.

Also make sure to run the file like this

                                                                       #
python3 mydiscordbot.py
                                                                       #

Instead of doing this

                                                                       #
python mydiscordbot.py
                                                                       #

I hope the fixes above fixed your problem. Thank you so much for reaching the end of this blog post.

Summing-up : 

The solutions above should be enough to solve your problem, I hope the article helped you get rid of the issue, please keep learning, keep coding and cheers.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/