You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解决Python中ImportError:无法从discord导入Enum的问题

修复 ImportError: cannot import name 'Enum' from 'discord' 错误

错误原因

问题根源是你的Python环境中同时安装了非官方的discord库和nextcord(discord.py的分支),wavelink或其依赖在导入时错误调用了discord库,但该库不存在Enum导出项,从而触发报错。

解决方案

  1. 卸载冲突的discord库
    执行命令彻底移除错误的discord包:

    pip uninstall discord -y
    

    同时确保nextcord为最新兼容版本:

    pip install --upgrade nextcord
    
  2. 匹配wavelink版本与代码
    你的代码使用的是wavelink旧版(v4及以下)API,若环境中安装了wavelink v5+会出现API不兼容问题,需对应调整:

    • 若想保留现有代码,安装适配的wavelink版本:
      pip install wavelink==4.0.0
      
    • 若升级到wavelink v5+,需修改代码适配新API(示例如下):
      import nextcord
      from nextcord.ext import commands
      import wavelink
      
      class Music(commands.Cog):
          def __init__(self, bot: commands.Bot):
              self.bot = bot
              self.bot.loop.create_task(self.connect_nodes())
      
          async def connect_nodes(self):
              await self.bot.wait_until_ready()
              # 新版节点连接方式
              node = wavelink.Node(uri="http://0.0.0.0:2333", password="yousheldnotpass")
              await wavelink.NodePool.connect(client=self.bot, nodes=[node])
      
          @commands.Cog.listener()
          async def on_wavelink_node_ready(self, node: wavelink.Node):
              print(f'Node [{node.id}] - ✅')
      
          @commands.command()
          async def play(self, ctx: commands.Context, *, search: str):
              if not ctx.author.voice:
                  await ctx.send("你需要先加入语音频道!")
                  return
              if not ctx.voice_client:
                  vc: wavelink.Player = await ctx.author.voice.channel.connect(cls=wavelink.Player)
              else:
                  vc: wavelink.Player = ctx.voice_client
              # 新版曲目搜索逻辑
              tracks = await wavelink.YouTubeTrack.search(search)
              if not tracks:
                  await ctx.send("未找到匹配的曲目!")
                  return
              await vc.play(tracks[0])
      
      def setup(bot):
          bot.add_cog(Music(bot))
      
  3. 验证环境依赖
    执行pip list查看已安装包,确认输出中只有nextcord和对应版本的wavelink,无多余的discord库。

内容的提问来源于stack exchange,提问作者Xaxah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 19:15:32