Python Deployment
Deploy Python applications to production.
Deploy Python apps properly.
Requirements File
```bash # Create requirements.txt pip freeze > requirements.txt
Install from requirements pip install -r requirements.txt ```
Gunicorn (WSGI Server)
```bash # Install pip install gunicorn
Run Flask app gunicorn app:app
With workers gunicorn -w 4 -b 0.0.0.0:8000 app:app ```
Docker
```dockerfile # Dockerfile FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"] ```
```bash # Build and run docker build -t myapp . docker run -p 8000:8000 myapp ```
Environment Setup
```python # config.py import os
class Config: DEBUG = False TESTING = False DATABASE_URI = os.getenv('DATABASE_URI')
class ProductionConfig(Config): DEBUG = False
class DevelopmentConfig(Config): DEBUG = True
config = { 'development': DevelopmentConfig, 'production': ProductionConfig } ```
Systemd Service
```ini # /etc/systemd/system/myapp.service [Unit] Description=My Python App After=network.target
[Service] User=www-data WorkingDirectory=/var/www/myapp Environment="PATH=/var/www/myapp/venv/bin" ExecStart=/var/www/myapp/venv/bin/gunicorn -w 4 app:app
[Install] WantedBy=multi-user.target ```
Health Check Endpoint
```python from flask import Flask
app = Flask(__name__)
@app.route('/health') def health(): return {'status': 'healthy'}, 200
@app.route('/') def home(): return "Welcome to the app!" ```
Remember
- Use production WSGI server - Set DEBUG=False in production - Use environment variables - Monitor your application
> Congratulations! You've completed all Python tutorials from beginner to advanced!