Django requirements.txt
Managing Python dependencies for your project
📦 What is requirements.txt?
The requirements.txt file lists all Python packages your Django project needs. It ensures everyone working on the project uses the same package versions, making deployment and collaboration seamless.
# requirements.txt
Django==4.2.0
djangorestframework==3.14.0
psycopg2-binary==2.9.6
python-decouple==3.8
Why Use requirements.txt?
Reproducibility
Same packages everywhere
Easy Deployment
Install all dependencies at once
Team Collaboration
Everyone uses same versions
Documentation
Clear list of dependencies
🔹 Creating requirements.txt
Generate a requirements.txt file automatically from your current environment. This captures all installed packages and their exact versions, ensuring perfect reproducibility.
# Activate your virtual environment first
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
# Generate requirements.txt
pip freeze > requirements.txt
# View the file
cat requirements.txt
Generated requirements.txt:
asgiref==3.7.2
Django==4.2.0
djangorestframework==3.14.0
psycopg2-binary==2.9.6
python-decouple==3.8
pytz==2023.3
sqlparse==0.4.4
🔹 Basic requirements.txt Structure
A typical Django project requirements file includes core Django packages, database drivers, and any additional libraries your project uses. Keep it organized and commented for clarity.
# Core Django
Django==4.2.0
djangorestframework==3.14.0
# Database
psycopg2-binary==2.9.6
# Environment variables
python-decouple==3.8
# Image processing
Pillow==10.0.0
# API documentation
drf-yasg==1.21.7
# CORS headers
django-cors-headers==4.2.0
Version Specifiers:
- ==4.2.0: Exact version (recommended)
- >=4.2.0: Minimum version
- ~=4.2.0: Compatible version
- Django: Latest version (not recommended)
🔹 Installing from requirements.txt
Install all project dependencies from the requirements file with a single command. This is typically the first step when setting up a project on a new machine.
# Install all packages
pip install -r requirements.txt
# Upgrade all packages
pip install -r requirements.txt --upgrade
# Install in a virtual environment (recommended)
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Output:
Collecting Django==4.2.0
Downloading Django-4.2.0-py3-none-any.whl (8.0 MB)
Collecting djangorestframework==3.14.0
Downloading djangorestframework-3.14.0-py3-none-any.whl
Installing collected packages: Django, djangorestframework...
Successfully installed Django-4.2.0 djangorestframework-3.14.0
🔹 Common Django Packages
Essential packages frequently used in Django projects for various functionalities:
# Web Framework
Django==4.2.0
djangorestframework==3.14.0
# Database Drivers
psycopg2-binary==2.9.6 # PostgreSQL
mysqlclient==2.2.0 # MySQL
# Authentication & Security
djangorestframework-simplejwt==5.2.2
django-allauth==0.54.0
# File Handling
Pillow==10.0.0
django-storages==1.13.2
boto3==1.28.0 # AWS S3
# API Tools
drf-yasg==1.21.7 # Swagger docs
django-cors-headers==4.2.0
django-filter==23.2
# Task Queue
celery==5.3.1
redis==4.6.0
# Testing
pytest-django==4.5.2
factory-boy==3.3.0
# Utilities
python-decouple==3.8
requests==2.31.0
🔹 Updating requirements.txt
Keep your requirements file up to date as you add or remove packages:
# Install a new package
pip install django-cors-headers
# Update requirements.txt
pip freeze > requirements.txt
# Or manually add to requirements.txt
echo "django-cors-headers==4.2.0" >> requirements.txt
# Remove a package
pip uninstall package-name
pip freeze > requirements.txt
🔹 Multiple Requirements Files
Organize dependencies for different environments (development, production, testing) using multiple requirement files for better management and security.
# requirements/base.txt (shared packages)
Django==4.2.0
djangorestframework==3.14.0
psycopg2-binary==2.9.6
# requirements/dev.txt (development only)
-r base.txt
django-debug-toolbar==4.1.0
pytest-django==4.5.2
# requirements/prod.txt (production only)
-r base.txt
gunicorn==21.2.0
whitenoise==6.5.0
# Install development requirements
pip install -r requirements/dev.txt
# Install production requirements
pip install -r requirements/prod.txt
🔹 Best Practices
Tips for requirements.txt:
- ✅ Always use version pinning (==) for production
- ✅ Add comments to organize packages
- ✅ Keep requirements.txt in version control (Git)
- ✅ Use virtual environments to avoid conflicts
- ✅ Update regularly but test thoroughly
- ❌ Don't include system packages
- ❌ Don't use latest versions without testing