Python开发Discord Bot报错“discord模块未找到”求助
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 withpython(which points to Python 2.x or an older 3.x) but installed discord.py usingpip3(which targets Python 3.x).
To verify:- Run
python --versionandpip --versionin your terminal—make sure the version numbers align (e.g., both say 3.11.x). - Use
python -m pip listto check ifdiscord.pyshows up in the installed packages list.
Fix: Install the package directly to the Python version you’re using withpython -m pip install discord.py(orpython3 -m pip install discord.pyif that’s your correct version).
- Run
Make sure your virtual environment is activated
If you’re using a virtual environment (likevenv) 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.
- On Linux/macOS: Run
Double-check your import statement
It sounds silly, but typos happen! When you installdiscord.py, the module you import in your code is justdiscord—notdiscord.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.pyIf you need the latest features, use
pip install -U discord.pyto 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




