Skip to content
Puranode

Guide

Run a discord.py Bot: requirements.txt and .env

A discord.py bot that works on your computer but fails on a server is nearly always missing one of two things: its packages, or its token. List the library as discord.py in a requirements.txt at the project root, pinned to the versions you tested with, and read the token from an environment variable instead of a file in the project. ModuleNotFoundError: No module named 'discord' means the first step did not happen in the Python that runs your bot.

Published

On this page

FAQ

Frequently asked

Why do I get No module named 'discord' on my server?

The Python that runs the bot has no discord.py installed. Put discord.py in a file named exactly requirements.txt at the project root, check the host's log for the install step, and make sure your own file is not called discord.py.

Should I pip install discord or discord.py?

discord.py. The PyPI package named discord describes itself as a mirror package for discord.py and asks you to install discord.py instead. Either way, the module you import is discord.

Do I need to pin versions in requirements.txt?

You should. An unpinned name installs whatever is newest when the server installs it. pip freeze, run inside the bot's virtual environment, writes exact versions of everything installed, so every machine gets the versions you tested.

Where should my Discord bot token go?

In an environment variable, read with os.environ. Use a .env file loaded by python-dotenv only on your own computer, list it in .gitignore, and set the real value in your host's environment settings.

Which Python version does discord.py need?

discord.py requires Python 3.8 or higher. Use the same minor version locally as on your server, so pinned packages install the same way in both places.

How much does bot hosting cost at Puranode?

Hobby is €4.99 every 30 days for one bot on 0.5 vCPU and 512 MB of RAM, with auto-restart on crash. Standard is €9.99 for up to three bots on 1 vCPU and 1 GB. Prices include VAT.

Projects referenced

This guide names other people’s work. These are their own pages, so you get the current documentation rather than our summary of it.

  • discord.py: Introduction: Python 3.8 or higher, the install command, the voice extra and its Linux libraries, and the virtual environment steps.
  • discord.py on PyPI: The package name to install and the Python requirement.
  • discord on PyPI: The package's own description: a mirror package for discord.py that asks you to install discord.py instead.
  • discord.py API reference: Client.run, which blocks and sets up logging, and the LoginFailure and PrivilegedIntentsRequired errors.
  • Discord developer quick start: Reset Token on the Bot page, and never sharing a token or checking it into version control.
  • pip user guide: Requirements files, pip install -r, pip freeze for repeatable installs, and running pip as python -m pip.
  • Python: os.environ and os.getenv: How a Python process reads its environment, and that os.environ is captured when the os module is first imported.
  • Python: the module search path: The directory containing the script is the first place Python looks for imports, which is why a file named discord.py shadows the library.
  • Python: venv: Creating a virtual environment so a project's packages stay separate from the system's.
  • python-dotenv: load_dotenv reading a .env file into os.environ without overriding variables that already exist, and the advice to keep .env out of git.

Give the bot a home that stays on

Hobby runs one Python or Node bot on 0.5 vCPU and 512 MB, with auto-restart on crash, for €4.99 every 30 days. Standard runs up to three bots on 1 GB for €9.99. VAT included.