Python · Modules

ModuleNotFoundError: No module named …

Python cannot find a package your code imports. Either it is not installed where the bot runs, or it is installed under a different name from the one you import.

← All errors

What you are seeing

Python

ModuleNotFoundError: No module named 'discord'

Missing from requirements.txt

On a server that installs your packages from requirements.txt, as ours does on deploy, a package that is not in that file is not installed. Anything you installed by hand on your own machine has to be listed there too. The file must be called exactly requirements.txt and sit next to your main file.

requirements.txt

discord.py
python-dotenv
aiohttp

Install name is not import name

The name you pip install and the name you import are often different, and requirements.txt needs the install name. The ones bot developers meet most:

discord.py is imported as discord. python-telegram-bot is imported as telegram. python-dotenv is imported as dotenv. PyNaCl, needed for Discord voice, is imported as nacl. Pillow is imported as PIL. beautifulsoup4 is imported as bs4. PyYAML is imported as yaml.

So "No module named 'discord'" is fixed by adding discord.py to requirements.txt, not "discord", which is a different, unrelated package.

The wrong Python

On your own machine, pip can install into a different Python from the one that runs your bot, especially with several versions installed or a virtual environment that is not active. Install with python -m pip install, using the same python command you run the bot with, and the two always match.

FAQ

Questions

I get "cannot import name" instead.

Check you have not named one of your own files after the package, such as discord.py or telegram.py. Python imports your file instead of the library. Rename it.