Prerequisites
- Basic understanding of programming concepts 📝
- Python installation (3.8+) 🐍
- VS Code or preferred IDE 💻
What you'll learn
- Understand the concept fundamentals 🎯
- Apply the concept in real projects 🏗️
- Debug common issues 🐛
- Write clean, Pythonic code ✨
🎯 Introduction
Welcome to the world of Python dependency management! 🎉 Have you ever tried to run someone else’s Python project only to get a flood of “ModuleNotFoundError” messages? Or maybe you’ve shared your amazing project with a friend, and they couldn’t run it because they didn’t have the right packages installed?
That’s where requirements.txt
comes to the rescue! 🦸♂️ In this tutorial, we’ll explore how this simple yet powerful file can make your Python projects portable, reproducible, and professional.
By the end of this tutorial, you’ll be managing dependencies like a pro, making your projects easy to share and deploy! Let’s dive in! 🏊♂️
📚 Understanding requirements.txt
🤔 What is requirements.txt?
Think of requirements.txt
as a shopping list for your Python project! 🛒 Just like how a recipe lists all the ingredients you need to cook a delicious meal, requirements.txt
lists all the packages your Python project needs to run properly.
In Python terms, it’s a plain text file that contains a list of all the external packages (dependencies) your project uses, along with their version numbers. This means you can:
- ✨ Share your project with anyone, anywhere
- 🚀 Deploy to servers with confidence
- 🛡️ Ensure everyone uses the same package versions
- 📦 Set up new development environments quickly
💡 Why Use requirements.txt?
Here’s why developers love using requirements.txt:
- Reproducibility 🔄: Ensures your project works the same way everywhere
- Easy Setup 💻: New team members can get started with one command
- Version Control 📖: Lock down specific versions to avoid breaking changes
- Deployment Ready 🚀: Cloud platforms understand requirements.txt automatically
Real-world example: Imagine you’re building a weather app 🌤️ that uses requests
for API calls and matplotlib
for charts. Without requirements.txt, anyone trying to run your app would have to figure out which packages to install through trial and error!
🔧 Basic Syntax and Usage
📝 Creating Your First requirements.txt
Let’s start with a simple example:
# 📁 requirements.txt
requests==2.31.0 # 🌐 For making HTTP requests
numpy==1.24.3 # 🔢 For numerical operations
pandas==2.0.3 # 📊 For data manipulation
matplotlib==3.7.1 # 📈 For creating charts
💡 Explanation: Each line specifies a package name and its version. The ==
means “exactly this version” - ensuring everyone gets the same package version!
🎯 Common Version Specifiers
Here are the ways to specify versions:
# 📁 requirements.txt
# 🎯 Exact version
flask==2.3.2
# 📈 Minimum version
django>=4.2.0
# 📉 Maximum version
requests<=2.31.0
# 🎯 Compatible version (>= 1.4, < 2.0)
numpy~=1.4.0
# 🚀 Latest version (be careful!)
pytest
# 🎨 Version range
beautifulsoup4>=4.9.0,<5.0.0
🛠️ Installing from requirements.txt
Installing all dependencies is super easy:
# 🚀 Install all packages at once!
pip install -r requirements.txt
# 💡 Pro tip: Use in a virtual environment
python -m venv myenv # 🏗️ Create virtual environment
source myenv/bin/activate # 🔌 Activate it (Linux/Mac)
# or
myenv\Scripts\activate # 🔌 Activate it (Windows)
pip install -r requirements.txt # 📦 Install packages
💡 Practical Examples
🌐 Example 1: Web Scraping Project
Let’s build a requirements.txt for a web scraping project:
# 📁 requirements.txt for WebScraperPro 🕷️
# Web scraping essentials
requests==2.31.0 # 🌐 HTTP library
beautifulsoup4==4.12.2 # 🍲 HTML parser
lxml==4.9.3 # ⚡ Fast XML/HTML processor
# Data handling
pandas==2.0.3 # 📊 Data analysis
openpyxl==3.1.2 # 📈 Excel file support
# Automation & scheduling
selenium==4.11.2 # 🤖 Browser automation
schedule==1.2.0 # ⏰ Task scheduling
# Utilities
python-dotenv==1.0.0 # 🔐 Environment variables
colorama==0.4.6 # 🎨 Colored terminal output
Now let’s see it in action:
# 🕷️ web_scraper.py
import requests
from bs4 import BeautifulSoup
import pandas as pd
from colorama import init, Fore
# 🎨 Initialize colorama for pretty output
init()
def scrape_products(url):
"""🛒 Scrape product information from a website"""
print(Fore.GREEN + "🚀 Starting web scraper...")
# 🌐 Make request
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
# 📊 Extract data
products = []
for item in soup.find_all('div', class_='product'):
product = {
'name': item.find('h2').text,
'price': item.find('span', class_='price').text,
'rating': item.find('div', class_='rating').text
}
products.append(product)
# 💾 Save to Excel
df = pd.DataFrame(products)
df.to_excel('products.xlsx', index=False)
print(Fore.CYAN + f"✅ Scraped {len(products)} products!")
return products
# 🎮 Let's use it!
if __name__ == "__main__":
products = scrape_products("https://example-shop.com")
🤖 Example 2: Machine Learning Project
Let’s create requirements.txt for an ML project:
# 📁 requirements.txt for ML Predictor 🧠
# Core ML libraries
scikit-learn==1.3.0 # 🤖 Machine learning toolkit
tensorflow==2.13.0 # 🧠 Deep learning framework
numpy==1.24.3 # 🔢 Numerical computing
pandas==2.0.3 # 📊 Data manipulation
# Visualization
matplotlib==3.7.1 # 📈 Plotting library
seaborn==0.12.2 # 🎨 Statistical visualization
plotly==5.15.0 # 📊 Interactive plots
# Model deployment
flask==2.3.2 # 🌐 Web framework
gunicorn==21.2.0 # 🚀 Production server
joblib==1.3.1 # 💾 Model serialization
# Development tools
jupyter==1.0.0 # 📓 Interactive notebooks
black==23.7.0 # 🎨 Code formatter
pytest==7.4.0 # 🧪 Testing framework
Here’s how you might use these packages:
# 🧠 ml_predictor.py
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import joblib
import matplotlib.pyplot as plt
import seaborn as sns
class MLPredictor:
"""🤖 Machine Learning Predictor with pretty visualizations"""
def __init__(self):
self.model = RandomForestClassifier(n_estimators=100)
print("🧠 ML Predictor initialized!")
def train(self, data_path):
"""🎯 Train the model on data"""
# 📊 Load data
print("📊 Loading data...")
df = pd.read_csv(data_path)
# 🎨 Visualize data distribution
plt.figure(figsize=(10, 6))
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
plt.title("🔥 Feature Correlation Heatmap")
plt.tight_layout()
plt.savefig('correlation_heatmap.png')
# 🔪 Split data
X = df.drop('target', axis=1)
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# 🚀 Train model
print("🚀 Training model...")
self.model.fit(X_train, y_train)
# 📈 Calculate accuracy
accuracy = self.model.score(X_test, y_test)
print(f"✨ Model accuracy: {accuracy:.2%}")
# 💾 Save model
joblib.dump(self.model, 'ml_model.pkl')
print("💾 Model saved successfully!")
return accuracy
# 🎮 Test it out!
if __name__ == "__main__":
predictor = MLPredictor()
predictor.train('sample_data.csv')
🚀 Advanced Concepts
🧙♂️ Advanced Feature 1: Requirements with Extras
Sometimes packages have optional features you can install:
# 📁 requirements.txt with extras
# 🎯 Install requests with security extras
requests[security]==2.31.0
# 🧠 Install tensorflow with GPU support
tensorflow[gpu]==2.13.0
# 📊 Install pandas with all optional dependencies
pandas[all]==2.0.3
# 🎨 Multiple extras
matplotlib[all,dev]==3.7.1
🏗️ Advanced Feature 2: Multiple Requirements Files
For complex projects, use multiple requirements files:
# 📁 requirements-base.txt
# Core dependencies everyone needs
requests==2.31.0
pandas==2.0.3
numpy==1.24.3
# 📁 requirements-dev.txt
# Include base requirements
-r requirements-base.txt
# 🧪 Development tools
pytest==7.4.0
black==23.7.0
flake8==6.0.0
ipdb==0.13.13
# 📁 requirements-prod.txt
# Include base requirements
-r requirements-base.txt
# 🚀 Production dependencies
gunicorn==21.2.0
psycopg2-binary==2.9.7
redis==4.6.0
Use them like this:
# 👨💻 For development
pip install -r requirements-dev.txt
# 🚀 For production
pip install -r requirements-prod.txt
🎯 Advanced Feature 3: Constraints Files
Use constraints to set upper bounds across all dependencies:
# 📁 constraints.txt
# 🛡️ Security constraints - never exceed these versions
urllib3<2.0
cryptography<41.0
Pillow<10.0
# 📁 requirements.txt
# Use with constraints
--constraint constraints.txt
django==4.2.4
pillow==9.5.0 # Will respect <10.0 constraint
⚠️ Common Pitfalls and Solutions
😱 Pitfall 1: Forgetting to Update requirements.txt
# ❌ Wrong way - installing packages but not updating requirements.txt
pip install awesome-new-package
# ... forget to update requirements.txt ...
# 💥 Team member: "Why doesn't your code work?!"
# ✅ Correct way - always update after installing!
pip install awesome-new-package
pip freeze | grep awesome-new-package >> requirements.txt
# or better yet
echo "awesome-new-package==1.0.0" >> requirements.txt
🤯 Pitfall 2: Using pip freeze Blindly
# ❌ Dangerous - includes ALL packages, even sub-dependencies
pip freeze > requirements.txt
# Results in 100+ packages when you only use 5! 😰
# ✅ Better - manually maintain your direct dependencies
# Only list packages you directly import
# requirements.txt
flask==2.3.2 # Direct dependency ✅
requests==2.31.0 # Direct dependency ✅
# Don't include werkzeug, itsdangerous, etc. (Flask installs these)
😵 Pitfall 3: Version Conflicts
# ❌ This will cause conflicts!
# requirements.txt
django==3.2.0
django-rest-framework==3.14.0 # Requires Django>=4.0!
# ✅ Check compatibility first
# Use pip-tools or pip-compile for dependency resolution
# requirements.in
django>=4.0,<5.0
djangorestframework>=3.14.0
# Then compile to requirements.txt
pip-compile requirements.in
🛠️ Best Practices
- 🎯 Pin Versions in Production: Use exact versions (
==
) for stability - 📝 Add Comments: Explain why each package is needed
- 🛡️ Regular Updates: Keep dependencies updated for security
- 🎨 Organize by Category: Group related packages together
- ✨ Use Virtual Environments: Always isolate project dependencies
Here’s a well-organized requirements.txt:
# 📁 requirements.txt - Weather Analytics Platform 🌤️
# === Web Framework ===
flask==2.3.2 # 🌐 Main web framework
flask-cors==4.0.0 # 🔓 Cross-origin requests
flask-sqlalchemy==3.0.5 # 🗄️ Database ORM
# === Data Processing ===
pandas==2.0.3 # 📊 Data manipulation
numpy==1.24.3 # 🔢 Numerical operations
scipy==1.11.1 # 📈 Scientific computing
# === Weather APIs ===
requests==2.31.0 # 🌐 HTTP requests
python-weather==0.4.3 # 🌦️ Weather data fetching
# === Visualization ===
matplotlib==3.7.1 # 📈 Static plots
plotly==5.15.0 # 📊 Interactive charts
# === Database ===
psycopg2-binary==2.9.7 # 🐘 PostgreSQL adapter
redis==4.6.0 # ⚡ Caching layer
# === Utilities ===
python-dotenv==1.0.0 # 🔐 Environment variables
click==8.1.6 # 🖱️ CLI interface
🧪 Hands-On Exercise
🎯 Challenge: Build a Complete Project Setup
Create a Python project with proper dependency management:
📋 Requirements:
- ✅ Create a web API that fetches cryptocurrency prices
- 🏷️ Use Flask for the web framework
- 👤 Add user authentication with JWT
- 📅 Schedule price updates every hour
- 🎨 Include a CLI tool for manual updates
🚀 Bonus Points:
- Separate dev and prod requirements
- Add pre-commit hooks for code quality
- Create a Docker setup with requirements
💡 Solution
🔍 Click to see solution
# 📁 requirements-base.txt
# === Core Dependencies ===
flask==2.3.2 # 🌐 Web framework
flask-jwt-extended==4.5.2 # 🔐 JWT authentication
requests==2.31.0 # 🌐 API calls
python-dotenv==1.0.0 # 🔐 Environment config
click==8.1.6 # 🖱️ CLI interface
schedule==1.2.0 # ⏰ Task scheduling
# === Data Storage ===
redis==4.6.0 # ⚡ Caching
sqlalchemy==2.0.19 # 🗄️ Database ORM
# 📁 requirements-dev.txt
-r requirements-base.txt
# === Development Tools ===
pytest==7.4.0 # 🧪 Testing
pytest-cov==4.1.0 # 📊 Coverage reports
black==23.7.0 # 🎨 Code formatter
flake8==6.0.0 # 🔍 Linter
pre-commit==3.3.3 # 🪝 Git hooks
ipdb==0.13.13 # 🐛 Debugger
# 📁 requirements-prod.txt
-r requirements-base.txt
# === Production Server ===
gunicorn==21.2.0 # 🚀 WSGI server
psycopg2-binary==2.9.7 # 🐘 PostgreSQL
sentry-sdk==1.29.2 # 📊 Error tracking
# 🏗️ crypto_tracker.py
from flask import Flask, jsonify
from flask_jwt_extended import JWTManager, create_access_token, jwt_required
import requests
import redis
import schedule
import time
import threading
import click
from datetime import datetime
app = Flask(__name__)
app.config['JWT_SECRET_KEY'] = 'super-secret-key' # 🔐 Change in production!
jwt = JWTManager(app)
cache = redis.Redis(host='localhost', port=6379, decode_responses=True)
def fetch_crypto_prices():
"""💰 Fetch current cryptocurrency prices"""
print(f"🔄 Fetching crypto prices at {datetime.now()}")
# 🌐 Fetch from API
response = requests.get('https://api.coingecko.com/api/v3/simple/price',
params={'ids': 'bitcoin,ethereum', 'vs_currencies': 'usd'})
if response.status_code == 200:
prices = response.json()
# 💾 Cache the results
cache.setex('crypto_prices', 3600, str(prices))
print("✅ Prices updated successfully!")
return prices
return None
@app.route('/api/login', methods=['POST'])
def login():
"""🔐 User login endpoint"""
# In real app, verify credentials here!
access_token = create_access_token(identity='user123')
return jsonify({'token': access_token}), 200
@app.route('/api/prices')
@jwt_required()
def get_prices():
"""💰 Get cryptocurrency prices"""
# 🔍 Check cache first
cached = cache.get('crypto_prices')
if cached:
return jsonify(eval(cached)), 200
# 🌐 Fetch fresh data
prices = fetch_crypto_prices()
if prices:
return jsonify(prices), 200
return jsonify({'error': 'Failed to fetch prices'}), 500
@app.cli.command()
@click.option('--crypto', default='bitcoin', help='Cryptocurrency to check')
def check_price(crypto):
"""🖱️ CLI command to check crypto price"""
prices = fetch_crypto_prices()
if prices and crypto in prices:
price = prices[crypto]['usd']
click.echo(f"💰 {crypto.title()} price: ${price:,.2f}")
else:
click.echo(f"❌ Could not fetch {crypto} price")
def run_scheduler():
"""⏰ Run scheduled tasks"""
schedule.every().hour.do(fetch_crypto_prices)
while True:
schedule.run_pending()
time.sleep(60)
if __name__ == '__main__':
# 🚀 Start scheduler in background
scheduler_thread = threading.Thread(target=run_scheduler)
scheduler_thread.daemon = True
scheduler_thread.start()
# 🌐 Run Flask app
app.run(debug=True)
# 📁 .pre-commit-config.yaml
repos:
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
# 🐳 Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements-prod.txt .
RUN pip install --no-cache-dir -r requirements-prod.txt
COPY . .
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "crypto_tracker:app"]
🎓 Key Takeaways
You’ve mastered dependency management! Here’s what you can now do:
- ✅ Create requirements.txt files with confidence 💪
- ✅ Manage package versions like a pro 🎯
- ✅ Organize complex projects with multiple requirement files 📁
- ✅ Avoid common pitfalls that trip up beginners 🛡️
- ✅ Deploy projects anywhere with reproducible environments 🚀
Remember: Good dependency management is the foundation of professional Python development. It’s the difference between “works on my machine” and “works everywhere”! 🤝
🤝 Next Steps
Congratulations! 🎉 You’ve conquered requirements.txt and dependency management!
Here’s what to do next:
- 💻 Create a requirements.txt for your current project
- 🏗️ Try the exercise above with different APIs
- 📚 Learn about
pip-tools
andpoetry
for advanced dependency management - 🌟 Share your projects with confidence!
Keep exploring, keep building, and remember - every Python expert started exactly where you are now. You’ve got this! 🚀
Happy coding! 🎉🚀✨