Github Codespaces导入discord_components报语法/导入错误求助
Discord机器人导入discord_components报错问题
最新报错情况
编辑补充:
按照xcodz-dot给出的建议操作后,原有问题已部分修复,但client.py中触发了新的异常,报错追踪信息如下:
Traceback (most recent call last): File "/workspaces/[repo_name]/resource.py", line 5, in <module> from discord_components import DiscordComponents, ButtonStyle, Button File "/opt/python/3.10.4/lib/python3.10/site-packages/discord_components/__init__.py", line 1, in <module> from .client import * File "/opt/python/3.10.4/lib/python3.10/site-packages/discord_components/client.py", line 3, in <module> from discord import ( ImportError: cannot import name 'Guild' from 'discord' (/opt/python/3.10.4/lib/python3.10/site-packages/discord/__init__.py)
问题定性:该报错属于依赖版本不匹配的独立故障,和你之前在
compat.py、compatibility.py中将asyncio.async修改为getattr(asyncio, "async")的操作没有任何关联。
已初步修复的旧问题
使用Github Codespaces开发Discord机器人程序时,导入discord_components触发SyntaxError,Linux bash终端报错追踪如下:
@[username]➜ /workspaces/[repo_name] (main ✗) $ /opt/python/latest/bin/python /workspaces/SineBot/resource.py Traceback (most recent call last): File "/workspaces/[repo_name]/resource.py", line 4, in <module> from discord_components import DiscordComponents, ButtonStyle, Button File "/opt/python/3.10.4/lib/python3.10/site-packages/discord_components/__init__.py", line 1, in <module> from .client import * File "/opt/python/3.10.4/lib/python3.10/site-packages/discord_components/client.py", line 3, in <module> from discord import ( File "/opt/python/3.10.4/lib/python3.10/site-packages/discord/__init__.py", line 20, in <module> from .client import Client, AppInfo, ChannelPermissions File "/opt/python/3.10.4/lib/python3.10/site-packages/discord/client.py", line 38, in <module> from .state import ConnectionState File "/opt/python/3.10.4/lib/python3.10/site-packages/discord/state.py", line 36, in <module> from . import utils, compat File "/opt/python/3.10.4/lib/python3.10/site-packages/discord/compat.py", line 32 create_task = asyncio.async ^^^^^ SyntaxError: invalid syntax
已尝试的排查操作
- 测试了相关技术问题下的所有公开解决方案
- 排除Python 3.5降级方案:项目依赖Python 3.10特性,降级成本极高无法落地
- 排除不存在的rewrite.zip安装包方案
- 调整导入顺序,先导入
discord.py再导入discord_components,仍触发完全相同的异常 - 同版本代码在本地设备运行时,
discord.py可正常加载
触发旧语法错误的compat.py(原文笔误写为combat.py)对应代码片段如下:
... import concurrent.futures import asyncio try: create_task = asyncio.ensure_future except AttributeError: create_task = asyncio.async # -> 异常触发位置 try: run_coroutine_threadsafe = asyncio.run_coroutine_threadsafe except AttributeError: ...
根因与解决方案
问题根因
两个报错的核心原因都是依赖版本错配:
- 当前环境安装的是v0.x远古版本的discord.py,该版本使用了Python 3.7+已设为保留关键字的
async作为asyncio模块的属性名,在Python 3.10环境下直接触发语法错误。之前修改源码绕过语法检查的操作只是临时补丁,没有解决根本问题。 - 安装的
discord_components是适配v1.0+版本discord.py的发行版,新版discord.py将Guild类放到了顶层导入路径,旧版v0.x discord.py没有这个顶层导出,所以绕过语法错误后立刻触发导入失败。
修复步骤
不需要手动修改任何依赖包的源码,按以下步骤重装正确版本的依赖即可*(全程不需要改compat.py等内置文件)*:
- 卸载当前环境中所有错装的discord相关包,执行命令:
pip uninstall -y discord.py discord discord-components - 安装适配Python 3.10的稳定版兼容依赖:
pip install discord.py==2.3.2 discord-components==2.1.1 - 重装完成后直接运行原有代码,两个报错都会自动消失。
内容的提问来源于stack exchange,提问作者Anony Mous
相关产品推荐
相关产品推荐

