You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python开发Discord Bot报错“discord模块未找到”求助

Fixing "discord module not found" Error When Running Your Discord Bot in Python

Hey there! I’ve dealt with this exact frustrating issue before, so let’s break down the most common fixes that usually get your bot back on track.

  • Check if your Python and pip versions match
    A super common culprit is having multiple Python versions installed on your system. For example, you might be running your script with python (which points to Python 2.x or an older 3.x) but installed discord.py using pip3 (which targets Python 3.x).
    To verify:

    • Run python --version and pip --version in your terminal—make sure the version numbers align (e.g., both say 3.11.x).
    • Use python -m pip list to check if discord.py shows up in the installed packages list.
      Fix: Install the package directly to the Python version you’re using with python -m pip install discord.py (or python3 -m pip install discord.py if that’s your correct version).
  • Make sure your virtual environment is activated
    If you’re using a virtual environment (like venv) to isolate your project dependencies, forgetting to activate it means your script can’t access the discord.py you installed there.
    Fix:

    • On Linux/macOS: Run source venv/bin/activate
    • On Windows: Run venv\Scripts\activate
      Once activated, you’ll see the environment name in your terminal prompt—then run your bot script again.
  • Double-check your import statement
    It sounds silly, but typos happen! When you install discord.py, the module you import in your code is just discord—not discord.py.
    Correct example:

    import discord
    from discord.ext import commands
    
    # Initialize your bot
    intents = discord.Intents.default()
    intents.message_content = True
    bot = commands.Bot(command_prefix='!', intents=intents)
    

    Avoid writing import discord.py—that will throw an error every time.

  • Reinstall discord.py to fix corrupted installations
    Sometimes the initial install gets messed up. Uninstall and reinstall the package to rule this out:

    pip uninstall discord.py
    pip install discord.py
    

    If you need the latest features, use pip install -U discord.py to update to the newest version.

Start with the Python/pip version check—it’s the most likely fix. If that doesn’t work, work through the other steps one by one. You’ll have your bot running before you know it!

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

火山引擎 最新活动