运行自制Discord机器人遇TypeError:禁止从URL类继承,求解决方法
Hey there, let's tackle this Discord bot import error you're facing. From the partial traceback you shared, it looks like the issue pops up when importing the discord module, specifically when trying to load the client components. Here are some actionable fixes to try out:
Discord.py (the library powering your bot) dropped support for Python 3.6 a while ago—this is almost certainly a key issue here. You have two options:
- Upgrade Python: Move to at least Python 3.8 (the minimum supported version for recent discord.py releases). After upgrading, confirm your version with
python --versionto ensure it's properly set up. - Use a Legacy Discord.py Version: If you can't upgrade Python, install the last discord.py release that supports 3.6 with this command:
pip install discord.py==1.7.3
Partial downloads or corrupted installations often cause import failures. Follow these steps:
- Uninstall the existing module first:
pip uninstall discord.py - Clear cached packages to avoid leftover issues:
pip cache purge - Reinstall the module (use the legacy version for Python 3.6, or the latest version if you upgraded Python):
# For Python 3.6 pip install discord.py==1.7.3 # For newer Python versions pip install discord.py
If you're working in a virtual environment, it's easy to accidentally install packages in your global Python instead of the venv. Fix this by:
- Activating your venv (Windows example):
.\venv\Scripts\activate - Reinstalling discord.py while the venv is active, using the command matching your Python version.
Discord.py relies on aiohttp and websockets for network communication. Outdated versions of these can break imports. Update them with:
pip install -U aiohttp websockets
Give these steps a try one by one—most likely the Python version or corrupted installation is the root cause.
内容的提问来源于stack exchange,提问作者Jeriel Fernandez

