Skip to main content

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

FlagPurpose
--onefileSingle executable file
--windowedNo console window (GUI apps)
--name NAMECustom executable name
--icon ICON.icoCustom icon (Windows)
--add-data SRC:DSTInclude 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

FeatureRenderPythonAnywhereRailway
Free TierYes (750 hrs/mo)Yes (limited)Yes ($5 credit)
Sleep Time15 min inactivityNever sleepsVaries
Custom DomainYesPaid plansYes
Auto-deploy from GitYesManualYes
DatabasePostgreSQL, RedisMySQL, SQLitePostgreSQL, MySQL
Best ForProduction appsLearning, small appsQuick 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.txt to 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 (from pip-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

PlatformHow to Set
RenderDashboard → Environment → Add Variable
PythonAnywhereWeb tab → WSGI config file → Add os.environ[]
RailwayDashboard → Variables → Add
Streamlit CloudApp 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


Deployment is the bridge between "it works on my machine" and "it works for everyone." Cross it with confidence!