Python Deployment Guide - From Code to Production 🚀
You have built a Python application. Now what? This guide walks through deployment options for different types of Python applications: CLI tools, web apps, and data dashboards.
Deployment Options Flowchart
1. Deploy a CLI Tool with PyInstaller
PyInstaller packages your Python script into a standalone executable that runs on machines without Python installed.
Step-by-Step
# Step 1: Install PyInstaller
pip install pyinstaller
# Step 2: Package your script
pyinstaller --onefile my_script.py
# Step 3: Find the executable in dist/
# ./dist/my_script (Linux/macOS)
# .\dist\my_script.exe (Windows)
Useful Flags
| Flag | Purpose |
|---|---|
--onefile | Single executable file |
--windowed | No console window (GUI apps) |
--name NAME | Custom executable name |
--icon ICON.ico | Custom icon (Windows) |
--add-data SRC:DST | Include data files |
Example: Deploy the File Organizer
pyinstaller --onefile --name organize organizer.py
# dist/organize is now a standalone executable
2. Deploy a Web App
Deployment Platforms Comparison
| Feature | Render | PythonAnywhere | Railway |
|---|---|---|---|
| Free Tier | Yes (750 hrs/mo) | Yes (limited) | Yes ($5 credit) |
| Sleep Time | 15 min inactivity | Never sleeps | Varies |
| Custom Domain | Yes | Paid plans | Yes |
| Auto-deploy from Git | Yes | Manual | Yes |
| Database | PostgreSQL, Redis | MySQL, SQLite | PostgreSQL, MySQL |
| Best For | Production apps | Learning, small apps | Quick prototypes |
Step-by-Step: Deploy a Flask App to Render
1. Create your Flask app
# app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Render!"
if __name__ == "__main__":
app.run()
2. Create requirements.txt
Flask==3.1.0
gunicorn==23.0.0
3. Create runtime configuration
# Procfile (no extension)
web: gunicorn app:app
4. Create Render account and deploy
# Push to GitHub first, then:
# 1. Go to dashboard.render.com
# 2. Click "New +" → "Web Service"
# 3. Connect your GitHub repo
# 4. Settings:
# - Runtime: Python 3
# - Build Command: pip install -r requirements.txt
# - Start Command: gunicorn app:app
# 5. Click "Deploy"
Your app will be live at https://your-app.onrender.com within minutes.
3. Deploy a Data Dashboard (Streamlit Cloud)
Streamlit turns data scripts into interactive web apps with minimal code.
Example Dashboard
# dashboard.py
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
st.title("Sales Dashboard")
uploaded_file = st.file_uploader("Upload CSV", type="csv")
if uploaded_file:
df = pd.read_csv(uploaded_file)
st.dataframe(df)
col = st.selectbox("Select column", df.columns)
fig, ax = plt.subplots()
df[col].hist(ax=ax)
st.pyplot(fig)
Deploy to Streamlit Cloud
# 1. Create requirements.txt
echo "streamlit==1.40.0\npandas==2.2.0\nmatplotlib==3.9.0" > requirements.txt
# 2. Push to GitHub
# 3. Go to share.streamlit.io
# 4. "Deploy an app" → connect GitHub
# 5. Set:
# - Repository: your/repo
# - Branch: main
# - Main file: dashboard.py
# 6. Click "Deploy"
📋 Requirements.txt Best Practices
# Pin exact versions for reproducibility
Flask==3.1.0
gunicorn==23.0.0
# Use >= for loose compatibility
requests>=2.31.0
# Separate dev dependencies (use requirements-dev.txt)
# requirements-dev.txt
pytest==8.3.0
black==24.4.0
Best practices:
- Use
pip freeze > requirements.txtto capture exact versions - Remove packages not directly imported (they are transitive dependencies)
- Test on a clean environment:
pip install -r requirements.txt - Use
pip-compile(frompip-tools) for complex projects
🔐 Environment Variables Guide
Never hardcode secrets. Use environment variables for sensitive data.
In Python
import os
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///default.db")
SECRET_KEY = os.getenv("SECRET_KEY")
API_KEY = os.getenv("API_KEY")
if not SECRET_KEY:
raise ValueError("SECRET_KEY environment variable not set")
Local Development (.env file)
# .env (DO NOT commit this file)
SECRET_KEY=your-secret-key-here
DATABASE_URL=postgresql://user:pass@localhost/db
API_KEY=abc123
# Load .env in development
from dotenv import load_dotenv
load_dotenv()
Setting Environment Variables on Platforms
| Platform | How to Set |
|---|---|
| Render | Dashboard → Environment → Add Variable |
| PythonAnywhere | Web tab → WSGI config file → Add os.environ[] |
| Railway | Dashboard → Variables → Add |
| Streamlit Cloud | App settings → Secrets → TOML format |
.gitignore Checklist
.env
*.pyc
__pycache__/
dist/
build/
*.spec
.virtualenv/
venv/
🚀 Quick Start: Deploy Checklist
[ ] Application runs locally without errors
[ ] requirements.txt is up to date
[ ] All secrets moved to environment variables
[ ] Procfile or start command configured
[ ] .gitignore excludes sensitive files
[ ] Repository pushed to GitHub
[ ] Account created on deployment platform
[ ] Database configured (if needed)
[ ] Deployed and tested on live URL
🔗 Related Resources
- Flask Web Apps — Build web apps with Flask
- CLI Tools — Build command-line apps
- Streamlit Data Apps — Interactive data apps
- Environment Management — Virtual environments and packages
- Database Integration — Connect to databases
Deployment is the bridge between "it works on my machine" and "it works for everyone." Cross it with confidence!