create_task = asyncio.async:语法错误:语法无效

问题描述 投票:0回答:6

我正在为 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。

discord discord.py
6个回答
27
投票

错误从何而来?

您使用的discord.py版本不支持Python 3.7(其中

async
成为保留关键字),如此问题中所述。 这一版本的 Discord.py 是 GitHub 存储库上的默认分支,遗憾的是它是由 Pip 安装的。

如何解决

您可以:

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

10
投票

您可以手动编辑文件并将该行从

create_task = asyncio.async
更改为
create_task = getattr(asyncio, 'async')

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


1
投票

作为快速修复,您可以在已安装的违规模块中将

asyncio.async
更改为
asyncio.ensure_future
并运行它。显然,正确的做法是更新模块,但是当不可能时,上述方法将使其再次运行。


0
投票

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

它仅与 Python 3.3 相关,Python 3.3 的 stdlib 中不包含 asyncio。


0
投票
修复它

pip install --upgrade aiohttp pip install --upgrade websockets
    

0
投票
@ertermishakk 的答案对我有用,我在 WebSocket 库方面遇到了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.