Python [Invalid syntax] with async def

Question:

I am trying write discord bots using Python, I have come across and threw together this bot.

import discord
import asyncio
import random

client = discord.Client()
inEmail = input("Email:")
inPassword = input("Passwd:")

async def background_loop():
    await client.wait_until_ready()
    while not client.is_closed:
        channel = client.get_channel("************")
        messages = ["Hello!", "How are you doing?", "Testing!!"]
        await client.send_message(channel, random.choice(messages))
        await asyncio.sleep(120)

client.loop.create_task(background_loop())
client.run(inEmail, inPassword)

Yet when I tried to run it, I received a SyntaxError:

File "1.py", line 7
  async def background_loop():
     ^
SyntaxError: invalid syntax

Why is that? I have never received that before when I tested it.

Asked By: Baiqing

||

Answers:

Updated answer for 2021 (discord.py 1.x – 2.x):

discord.py currently supports Python 3.5 and up. If you receive a SyntaxError, it means you’re using an older version of Python and isn’t supported by discord.py.

First, install a later version of Python (3.8.x is preferred as of this message), and run python3.8 bot.py or py -3.8 bot.py (for Windows) when running your bot. Note: when installing, don’t forget to choose "Add Python to PATH".


Original Answer

Asynchronous requests were introduced to Python in v3.3, if you’re running Python prior to v3.3 (including v2.X), you’ll have to install a newer version of Python.


Only if you are running Python 3.3: asyncio is not part of the stdlib, you’ll need to install it manually from pypi:

pip install asyncio

The async and await keywords are only valid for Python 3.5 or newer. If you’re using Python 3.3 or 3.4, you will need to make the following changes to your code:

  1. Use the @asyncio.coroutine decorator instead of the async statement:
async def func():
    pass

# replace to:

@asyncio.coroutine
def func():
    pass
  1. Use yield from instead of await:
await coroutine() 

# replace to:

yield from coroutine()

Here is an example of what your function need to change into (if you’re on 3.3-3.4):

import asyncio

@asyncio.coroutine 
def background_loop():
    yield from client.wait_until_ready()
    while not client.is_closed:
        channel = client.get_channel("************")
        messages = ["Hello!", "How are you doing?", "Testing!!"]
        yield from client.send_message(channel, random.choice(messages))
        yield from asyncio.sleep(120)

The aforementioned syntax is still supported in newer versions of Python 3, but it is recommended to use await and async if there’s no need to support for Python 3.3-3.4. You can refer back to this documentation, here’s a short snippet:

The async def type of coroutine was added in Python 3.5, and is
recommended if there is no need to support older Python versions.


Aside:

currently supports 3.4.2-3.6.6, (It does not support 3.3-3.4.1, 3.7 as of January 2019).

For developing with discord.py, I suggest using the discord.py rewrite branch:

supports 3.5.3-3.7.

Answered By: Taku

From version 3.7
async and await are reserved keywords

like the error in below image.

enter image description here

Copy and open the path (without __init__.py).
You will get a list of .py files
enter image description here

Rename async.py to _async.py or anything you want, as async is now a reserved keyword with us from version 3.7.

Once renamed, modify the new name everywhere.

*NOTE
Although it is not a permanent solution
but it worked for me in case of the same syntax error
while working with firebase.
Best solution is to go with previous version of Python. i.e version below 3.7.

Answered By: Er M S Dandyan

I solved it by installing the updated PyMC from github (they corrected the bug that happens in Python 3.7):

pip install git+https://github.com/pymc-devs/pymc.git

Answered By: Marcelo Tournier

If you’re on a Mac, try running the file with python3 discord_bot.py instead of python discord_bot.py, as python defaults to Version 2.7.

Answered By: Vishnu TM