create_task = asyncio.async:SyntaxError:语法无效

时间:2018-07-05 16:57:59

标签: discord discord.py

我正在为Discord创建一个机器人,我只是编写了以下简单代码:

import discord

TOKEN = "token"

client = discord.Client()


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


client.run(TOKEN)

并产生以下错误:

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

我在互联网上进行了搜索,大多数人都说使用Python 3.7,这就是我一直在使用的语言。另外,我一直在使用PyCharm作为我的Python IDE。

3 个答案:

答案 0 :(得分:16)

错误来自哪里?

您使用的discord.py版本不支持Python 3.7 (其中async成为保留关键字),如this issue中所述。 此版本的discord.py是GitHub repo上的默认分支,可悲的是Pip安装的版本。

如何修复

您可以:

  • 将您的Python版本降级为3.6。
  • 基于rewrite branch 正在开发中的另一个版本的discord.py ,例如,使用命令:python3 -m pip install --user -U https://github.com/Rapptz/discord.py/archive/rewrite.zip

答案 1 :(得分:5)

您可以手动编辑文件并将该行从create_task = asyncio.async更改为create_task = getattr(asyncio, 'async')

在此处查看更多信息:https://github.com/Rapptz/discord.py/issues/1249

答案 2 :(得分:0)

不要不要在您的需求中添加 asyncio,它已经在 Python 中(自 3.5 起)。

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