Python [Invalid syntax] with async def

Viewed 103032

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.

4 Answers

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.

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

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.

Related