【问题标题】:python 3.9 discord.py asyncio.async errorpython 3.9 discord.py asyncio.async 错误
【发布时间】:2021-02-12 14:21:33
【问题描述】:

这个问题涉及由 asyncio 引起的 discord.py 错误,但非常欢迎任何有关创建不和谐机器人的建议。我非常愿意阅读文档以充分理解回复,并将继续阅读提供的文章。

问题:

我正在尝试在 python 3.9 中创建一个简单的不和谐机器人。我是 python 新手,一直在使用 pycharm 编写脚本。我在尝试运行脚本时发现了一个错误。此处提供的脚本:

import discord

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

client.run('my bot token')

此处提供的错误:

Traceback (most recent call last):
  File "C:\Users\User\py_projects\discord\scrambot_main.py", line 2, in <module>
    import discord
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\__init__.py", line 20, in <module>
    from .client import Client, AppInfo, ChannelPermissions
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 38, in <module>
    from .state import ConnectionState
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\state.py", line 36, in <module>
    from . import utils, compat
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\compat.py", line 32
    create_task = asyncio.async
                          ^
SyntaxError: invalid syntax

我目前看到的解决方案:

我看到无数关于 python 3.7 的问题,而 3.9 没有。这可能是因为没有其他人在 pycharm 中遇到与 python 3.9 相同的错误,或者解决方案与 python 3.7 中的相同。

我看到很多人建议降到 python 3.6 版本,因为 async 是 3.7 中的关键字,我想尽可能避免使用它。我很确定它仍然是 3.9 中的关键字。

一些回复说 pycharm 创建虚拟环境并且不会安装 discord.py。在我的情况下这不是真的——我已经检查了我的项目首选项,并且我的脚本实际上包含并访问了 discord.py。更多证据在错误消息中:导致问题的行在 discord.py 目录中,因此编译器必须能够访问它。对吧?

问题可能源于我使用pip install discord.py 安装,然后使用pip3 install discord.py 再次尝试。据我所知,这些都按预期工作。如果我尝试使用我提供的pip3 install discordpip install discord,则会出现错误。我不知道 .py 是否有所作为,但如果有,我相信知道这一点会有所帮助。我见过的唯一其他版本的解决方案是从 github 下载 discord.py 的 rewrite 版本。我还没有尝试过,因为我想让这个版本在可能的情况下工作。

C:\Users\User>pip3 install discord
Collecting discord
  Using cached discord-1.0.1-py3-none-any.whl (1.1 kB)
Collecting discord.py>=1.0.1
  Using cached discord.py-1.5.1-py3-none-any.whl (701 kB)
Processing c:\users\user\appdata\local\pip\cache\wheels\b6\9c\bd\6b99bc6ec9dab11f3756d31fb8506d3ecf07aea58b6201f539\aiohttp-3.6.3-py3-none-any.whl
Collecting attrs>=17.3.0
  Using cached attrs-20.2.0-py2.py3-none-any.whl (48 kB)
Requirement already satisfied: chardet<4.0,>=2.0 in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from aiohttp<3.7.0,>=3.6.0->discord.py>=1.0.1->discord) (3.0.4)
Collecting yarl<1.6.0,>=1.0
  Using cached yarl-1.5.1.tar.gz (173 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Collecting multidict<5.0,>=4.5
  Using cached multidict-4.7.6.tar.gz (50 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Requirement already satisfied: async-timeout<4.0,>=3.0 in c:\users\user\appdata\local\programs\python\python39\lib\site-packages (from aiohttp<3.7.0,>=3.6.0->discord.py>=1.0.1->discord) (3.0.1)
Collecting idna>=2.0
  Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Building wheels for collected packages: yarl, multidict
  Building wheel for yarl (PEP 517) ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\user\appdata\local\programs\python\python39\python.exe' 'c:\users\user\appdata\local\programs\python\python39\lib\site-packages\pip\_vendor\pep517\_in_process.py' build_wheel 'C:\Users\User\AppData\Local\Temp\tmpib94rz6b'
       cwd: C:\Users\User\AppData\Local\Temp\pip-install-rrtk0pij\yarl
  Complete output (35 lines):
  **********************
  * Accellerated build *
  **********************
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-3.9
  creating build\lib.win-amd64-3.9\yarl
  copying yarl\_quoting.py -> build\lib.win-amd64-3.9\yarl
  copying yarl\_quoting_py.py -> build\lib.win-amd64-3.9\yarl
  copying yarl\_url.py -> build\lib.win-amd64-3.9\yarl
  copying yarl\__init__.py -> build\lib.win-amd64-3.9\yarl
  running egg_info
  writing yarl.egg-info\PKG-INFO
  writing dependency_links to yarl.egg-info\dependency_links.txt
  writing requirements to yarl.egg-info\requires.txt
  writing top-level names to yarl.egg-info\top_level.txt
  reading manifest file 'yarl.egg-info\SOURCES.txt'
  reading manifest template 'MANIFEST.in'
  warning: no previously-included files matching '*.pyc' found anywhere in distribution
  warning: no previously-included files matching '*.cache' found anywhere in distribution
  warning: no previously-included files found matching 'yarl\*.html'
  warning: no previously-included files found matching 'yarl\*.so'
  warning: no previously-included files found matching 'yarl\*.pyd'
  no previously-included directories found matching 'docs\_build'
  writing manifest file 'yarl.egg-info\SOURCES.txt'
  copying yarl\__init__.pyi -> build\lib.win-amd64-3.9\yarl
  copying yarl\_quoting_c.c -> build\lib.win-amd64-3.9\yarl
  copying yarl\_quoting_c.pyi -> build\lib.win-amd64-3.9\yarl
  copying yarl\_quoting_c.pyx -> build\lib.win-amd64-3.9\yarl
  copying yarl\py.typed -> build\lib.win-amd64-3.9\yarl
  running build_ext
  building 'yarl._quoting_c' extension
  error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
  ----------------------------------------
  ERROR: Failed building wheel for yarl
  Building wheel for multidict (PEP 517) ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\users\user\appdata\local\programs\python\python39\python.exe' 'c:\users\user\appdata\local\programs\python\python39\lib\site-packages\pip\_vendor\pep517\_in_process.py' build_wheel 'C:\Users\User\AppData\Local\Temp\tmp_yeznrct'
       cwd: C:\Users\User\AppData\Local\Temp\pip-install-rrtk0pij\multidict
  Complete output (40 lines):
  **********************
  * Accellerated build *
  **********************
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-3.9
  creating build\lib.win-amd64-3.9\multidict
  copying multidict\_abc.py -> build\lib.win-amd64-3.9\multidict
  copying multidict\_compat.py -> build\lib.win-amd64-3.9\multidict
  copying multidict\_multidict_base.py -> build\lib.win-amd64-3.9\multidict
  copying multidict\_multidict_py.py -> build\lib.win-amd64-3.9\multidict
  copying multidict\__init__.py -> build\lib.win-amd64-3.9\multidict
  running egg_info
  writing multidict.egg-info\PKG-INFO
  writing dependency_links to multidict.egg-info\dependency_links.txt
  writing top-level names to multidict.egg-info\top_level.txt
  reading manifest file 'multidict.egg-info\SOURCES.txt'
  reading manifest template 'MANIFEST.in'
  warning: no previously-included files matching '*.pyc' found anywhere in distribution
  warning: no previously-included files found matching 'multidict\_multidict.html'
  warning: no previously-included files found matching 'multidict\*.so'
  warning: no previously-included files found matching 'multidict\*.pyd'
  warning: no previously-included files found matching 'multidict\*.pyd'
  no previously-included directories found matching 'docs\_build'
  writing manifest file 'multidict.egg-info\SOURCES.txt'
  copying multidict\__init__.pyi -> build\lib.win-amd64-3.9\multidict
  copying multidict\_multidict.c -> build\lib.win-amd64-3.9\multidict
  copying multidict\py.typed -> build\lib.win-amd64-3.9\multidict
  creating build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\defs.h -> build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\dict.h -> build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\istr.h -> build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\iter.h -> build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\pair_list.h -> build\lib.win-amd64-3.9\multidict\_multilib
  copying multidict\_multilib\views.h -> build\lib.win-amd64-3.9\multidict\_multilib
  running build_ext
  building 'multidict._multidict' extension
  error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
  ----------------------------------------
  ERROR: Failed building wheel for multidict
Failed to build yarl multidict
ERROR: Could not build wheels for yarl, multidict which use PEP 517 and cannot be installed directly

问题:

    Has anyone had success with discord bots in python 3.9?
    Is something wrong with my script?
    Is async a keyword in python 3.9? If so, how can i modify my program to solve this problem?
    How can I fix this issue without downgrading to python 3.6? Is that the only way?
    Why does pip install discord not work? What are wheels? I do not want to install C++ build tools.
    Would it be easier to program a bot in C++ or JS? I don't know any JS but i am familiar with C++.

对于这个问题的任何帮助将不胜感激。另外,由于这是我的第一个堆栈溢出帖子,请随时让我知道我是否可以在提出这个问题时做得更好。如果我还可以提供其他任何东西来帮助那些愿意花时间解决像这样的菜鸟问题的优秀开发人员,请告诉我。

另外,如果愿意并且有能力,请提供一些编写不和谐机器人的技巧。这旨在成为一个小型管理机器人,用于测试和开发我的 python 技能。与人的互动是重点,而不是任何形式的自动化,尽管我确实想尝试尽可能多的想法。

编辑: 包括 PyCharm python 解释器设置。

在打开一个新的 venv 项目并将 discord.py 安装到它时也遇到了问题。与上面相同的车轮错误,在下图中提供了错误和其他相关窗口。也不确定将我的脚本文件放在哪里或如何进行运行配置。

【问题讨论】:

  • 您是否使用虚拟环境以及 PyCharm python 解释器设置中显示的版本(添加到您的帖子中)。您的代码在虚拟环境设置为 Python3.9 和包 discord.py 版本 1、5、1 的情况下成功地为我工作。
  • @DaveStSomeWhere 我尝试在创建时使用 venv 选项设置重复项目。我看不到任何地方可以放置我的代码。我应该把我的脚本放在哪个目录?
  • 看起来您使用的是 Windows,它希望您拥有 MS C++ 版本 14 或更高版本 - 我不是 Windows,因此无法提供任何输入,但请尝试根据错误修复问题消息。如果您查看最新版本列,您会看到 discord.py 升级到 1.5.1 的箭头 - 如果您突出显示该行并单击右侧的箭头,它应该会为您升级(当然您需要修复Windows 问题)。您的代码工作区在右下方的状态栏中显示了 venv,并且看起来是正确的。

标签: python python-3.x pycharm discord.py


【解决方案1】:

问题在于您的discord.Client(),您永远不应该使用它来代替使用

from discord.ext import commands

client = commands.Bot(prefix="ur prefix")

而且你的命令写错了

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

正确方法:

@client.command()
async def hello(ctx):
  await ctx.send("Hello!")

【讨论】:

  • 您能解释一下原因吗?你的回答似乎很有趣,但我不太明白你为什么建议你的解决方案
  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
  • @JosephBudin 是的,因为他们直接使用了客户端,现在该方法已被删除,并且他编写命令的方式不正确,正确的方法是我给了
  • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
猜你喜欢
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 2021-08-04
  • 2021-04-18
  • 2021-01-16
  • 2023-03-10
相关资源
最近更新 更多