21

I'm creating a bot for Discord, and I just wrote this simple code:

import discord

TOKEN = "token"

client = discord.Client()


@client.event
async def on_ready():
    print('Bot is ready.')


client.run(TOKEN)

and it produces the following error:

Traceback (most recent call last):
  File "/Users/pcaires/Desktop/Programação/Python/Discord Bots/Main.py", line 1, in <module>
    import discord
  File "/Users/pcaires/Desktop/Programação/Python/Discord Bots/venv/lib/python3.7/site-packages/discord/__init__.py", line 20, in <module>
    from .client import Client, AppInfo, ChannelPermissions
  File "/Users/pcaires/Desktop/Programação/Python/Discord Bots/venv/lib/python3.7/site-packages/discord/client.py", line 38, in <module>
    from .state import ConnectionState
  File "/Users/pcaires/Desktop/Programação/Python/Discord Bots/venv/lib/python3.7/site-packages/discord/state.py", line 36, in <module>
    from . import utils, compat
  File "/Users/pcaires/Desktop/Programação/Python/Discord Bots/venv/lib/python3.7/site-packages/discord/compat.py", line 32
    create_task = asyncio.async
                              ^
SyntaxError: invalid syntax

I searched and searched in the internet, and most of the people say to use Python 3.7, and that's what I've been using. Also, I've been using PyCharm as my IDE for Python.

SuperKooks
  • 473
  • 1
  • 4
  • 12
O Tal Antiquado
  • 283
  • 1
  • 2
  • 9
  • I suggest removing your token from the script as it means anyone can take control of your bot. I would also generate a new token or even create a new bot. – Benjin Jul 06 '18 at 06:43
  • This question is no longer relevant for discord.py, if you have an invalid syntax error pointing to async def, see this post instead: https://stackoverflow.com/questions/43948454/python-invalid-syntax-with-async-def. – Taku Oct 25 '21 at 08:55

5 Answers5

27

Where does the error come from?

The version of discord.py you are using does not support Python 3.7 (in which async becomes a reserved keyword), as explained in this issue. This version of discord.py, which is the default branch on the GitHub repo, is sadly the one installed by Pip.

How to fix it

You can either:

  • downgrade your version of Python to 3.6.
  • install another version of discord.py, based on the rewrite branch which is under active development, for example with the command : python3 -m pip install --user -U https://github.com/Rapptz/discord.py/archive/rewrite.zip
cmousset
  • 625
  • 7
  • 21
10

You can manually edit the file and change that line from create_task = asyncio.async to create_task = getattr(asyncio, 'async')

See more info here: https://github.com/Rapptz/discord.py/issues/1249

vhs
  • 117
  • 2
  • 2
    I think the more relevant advice from that link is to update to a more recent version of `discord.py` that is compatible with python 3.7 – Patrick Haugh Jul 05 '18 at 17:42
  • 7
    This isn't a good (actually it's a terrible) recommendation. First, you shouldn't manually change site-package source files, it makes your code dependent on an altered requirement package. Second, `asyncio.async` was deprecated versions ago, so if you're going to recommend a fix, change it to something that's not deprecated. Third, there's clearly a better choice of solutions, see Patrick’s comment (there was an semi-official patch for this very issue — though the patch is like your comment, I don't believe that will get passed to the official build, see the previous two points). – Taku Jul 05 '18 at 23:36
1

As a quick fix you can change asyncio.async to asyncio.ensure_future in the installed offending module and run it. Obviously the right thing to do is get the module updated, but when that's not possible the above will get it running again.

matth
  • 563
  • 7
  • 22
0

Do NOT add asyncio in your requirements, it's already in Python (since 3.5).

It is only relevant for Python 3.3, which does not include asyncio in its stdlib.

freedev
  • 25,946
  • 8
  • 108
  • 125
0

fix it with

pip install --upgrade aiohttp
pip install --upgrade websockets
ertemishakk
  • 517
  • 3
  • 14