Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

discord - create_task = asyncio.async: SyntaxError: invalid syntax

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/Programac?a?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/Programac?a?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/Programac?a?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/Programac?a?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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...