Before you deploy
Deploying a Python Discord bot means moving it from your local machine to a server where it runs continuously without your computer being on. Before deployment, make sure your bot runs correctly locally. Test every command, verify error handling works, and confirm the bot connects to Discord without issues.
Prerequisites
- A working Discord bot built with Python (discord.py, Nextcord, Pycord, or another library)
- Python 3.9 or later installed locally
- A Discord bot application created in the Developer Portal
- Your bot token stored securely
- An SFTP client like FileZilla or WinSCP
Project structure
A well-organised project makes deployment straightforward. Before uploading, your project should look like this:
my-discord-bot/
├── cogs/ # Command groups (if using cogs)
│ ├── moderation.py
│ └── utility.py
├── .env # Environment variables
├── .gitignore # Excludes .env and venv
├── bot.py # Main entry point
└── requirements.txt # Pinned dependencies
Managing dependencies
Creating requirements.txt
Your requirements.txt file tells the hosting platform exactly which packages your bot needs. Generate it from your local environment:
# If using a virtual environment (recommended)
pip freeze > requirements.txt
For a cleaner file, manually list only your direct dependencies with pinned versions:
discord.py==2.3.2
python-dotenv==1.0.1
aiohttp==3.9.3
Pinning versions prevents unexpected breakage when a dependency releases a new version. Your bot should install the exact same versions on the server as on your local machine.
Virtual environments
Always develop in a virtual environment locally. This keeps your bot's dependencies separate from your system Python:
# Create virtual environment
python -m venv venv
# Activate (Linux/macOS)
source venv/bin/activate
# Activate (Windows)
venv\Scripts\activate
# Install dependencies
pip install discord.py python-dotenv
Do not upload the venv/ directory to your hosting platform. It contains platform-specific binaries that will not work on the server. Upload requirements.txt instead and install dependencies on the server.
Environment variables
Never put your bot token in your Python source code. Use environment variables loaded from a .env file.
Setting up python-dotenv
pip install python-dotenv
Create a .env file:
DISCORD_TOKEN=your_bot_token_here
PREFIX=!
OWNER_ID=123456789012345678
Load it in your bot:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
bot = discord.Bot(intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}')
@bot.slash_command(description="Check if the bot is online")
async def ping(ctx):
await ctx.respond(f'Pong! Latency: {round(bot.latency * 1000)}ms')
bot.run(os.getenv('DISCORD_TOKEN'))
For more on managing environment variables across different setups, read our environment variables guide.
Deploying to MonkeyBytes
MonkeyBytes provides a pre-configured Python environment with 1 GB RAM, 1 GB SSD storage, and 150% CPU. The platform handles Python installation and process management.
Step 1: Create your bot instance
Log in to the MonkeyBytes dashboard and create a new server. Select the Python runtime. Your server is provisioned with SFTP credentials displayed on the dashboard.
Step 2: Enable maintenance mode
Enable maintenance mode from the dashboard before uploading files. This prevents the server from trying to run incomplete code during the upload process.
Step 3: Upload via SFTP
Connect using your SFTP credentials and upload:
bot.py(or your main entry file)requirements.txt.env(with your bot token)- Any additional source files (cogs/, utils/, etc.)
Do not upload venv/, __pycache__/, or .pyc files.
Step 4: Install dependencies
Use the web console in the dashboard to install your packages:
pip install -r requirements.txt
Step 5: Configure the start command
Set your start command in the dashboard:
python bot.py
If your main file has a different name, adjust accordingly. Some bots use python3 explicitly, but on MonkeyBytes the Python runtime is pre-configured so python points to the correct version.
Step 6: Disable maintenance mode and start
Disable maintenance mode and start your bot. The real-time console shows output. You should see your on_ready event fire.
Choosing your Discord library
Python has several Discord API libraries. Each deploys the same way but has different features:
| Library | Package name | Slash commands | Status |
|---|---|---|---|
| discord.py | discord.py |
Yes (v2.0+) | Active, most popular |
| Pycord | py-cord |
Yes (built-in) | Active fork with extra features |
| Nextcord | nextcord |
Yes | Active fork |
| interactions.py | discord-py-interactions |
Yes (slash-first) | Active, slash-focused |
Important: do not install multiple Discord libraries in the same project. They conflict because they share the discord namespace. Pick one and use it exclusively. For a detailed comparison of Python versus Node.js for bot development, read our Node.js vs Python comparison.
Common deployment issues
ModuleNotFoundError
If your bot crashes with ModuleNotFoundError: No module named 'xyz', the package is not installed on the server. Check that it is listed in requirements.txt and run pip install -r requirements.txt again.
Wrong Python version
discord.py 2.x requires Python 3.8 or later. If you use newer Python features like match statements (3.10+) or exception groups (3.11+), ensure your hosting platform provides a compatible version. MonkeyBytes provides a current Python version.
Async runtime errors
Python Discord bots are asynchronous. Common errors include calling async functions without await, blocking the event loop with synchronous operations like time.sleep() (use asyncio.sleep() instead), and running CPU-heavy tasks on the main thread.
Token not loading
Verify your .env file is in the same directory as your main Python file. Check that load_dotenv() is called before os.getenv('DISCORD_TOKEN'). Debug with:
import os
from dotenv import load_dotenv
load_dotenv()
print('Token loaded:', bool(os.getenv('DISCORD_TOKEN')))
pip install failures
Some Python packages require C compilation. If pip install fails with compilation errors, the package may need system-level build tools. On managed hosting like MonkeyBytes, common packages are supported. For packages requiring unusual system dependencies, a VPS may be necessary.
Deploying to a VPS
For bots that need custom system packages or more control, a VPS provides full flexibility.
Server setup
# Update system
sudo apt update && sudo apt upgrade -y
# Install Python and pip
sudo apt install -y python3 python3-pip python3-venv
# Create project directory
mkdir ~/my-discord-bot && cd ~/my-discord-bot
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
Running with systemd
Create a systemd service file for automatic start and restart:
# /etc/systemd/system/discord-bot.service
[Unit]
Description=My Discord Bot
After=network.target
[Service]
Type=simple
User=botuser
WorkingDirectory=/home/botuser/my-discord-bot
ExecStart=/home/botuser/my-discord-bot/venv/bin/python bot.py
Restart=always
RestartSec=10
EnvironmentFile=/home/botuser/my-discord-bot/.env
[Install]
WantedBy=multi-user.target
# Enable and start
sudo systemctl daemon-reload
sudo systemctl enable discord-bot
sudo systemctl start discord-bot
# Check status
sudo systemctl status discord-bot
# View logs
journalctl -u discord-bot -f
For a detailed comparison of VPS versus managed hosting, read our VPS vs free hosting guide.
Keeping your bot updated
On MonkeyBytes
- Enable maintenance mode
- Upload changed files via SFTP
- Run
pip install -r requirements.txtif dependencies changed - Disable maintenance mode and restart
On a VPS
- SSH into your server
- Activate the virtual environment
- Pull or upload new code
- Run
pip install -r requirements.txtif needed - Restart with
sudo systemctl restart discord-bot
Ready to deploy? Get started on MonkeyBytes and have your Python bot online in minutes. For the Node.js equivalent, read our Node.js deployment guide. For security during deployment, see our security guide.