Run a Python app (Django, Flask)

python, flask, django, passenger_wsgi, virtualenv, wsgi · 0 found this helpful

The Node.js and Python apps card in Developer runs Flask, Django, and other WSGI apps behind your domain. It creates an isolated virtual environment for each app.

Before you start

Create the app

  1. Open Account → Hosting → your site → Developer.
  2. Under Node.js and Python apps, click New app.
  3. Fill in:
Field Example
Runtime Python
Python version The newest 3.x on the list (3.13 at the time of writing)
Address app.example.ca, with a path if you want one
Code folder apps/bakery
Startup file passenger_wsgi.py
  1. Click Create app.
Node.js and Python apps in Developer

Passenger looks for a callable named application in the startup file. The examples below define it.

Flask example

~/apps/bakery/app.py:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/")
def home():
    return jsonify(ok=True, shop="North Shore Bakery")

@app.route("/health")
def health():
    return "ok"

~/apps/bakery/passenger_wsgi.py:

# Passenger looks for a WSGI callable named "application".
from app import app as application

requirements.txt:

Flask==3.0.3
gunicorn==22.0.0

Django example

passenger_wsgi.py:

import os
import sys

# Make the project importable
sys.path.insert(0, os.path.dirname(__file__))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bakery.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

In settings.py:

import os
DEBUG = False
ALLOWED_HOSTS = ["app.example.ca"]
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
STATIC_ROOT = "/home/yourusername/public_html/static"
STATIC_URL = "/static/"

Run these over SSH to collect static files and set up the database:

source /home/yourusername/virtualenv/apps/bakery/3.12/bin/activate && cd /home/yourusername/apps/bakery
python manage.py collectstatic --noinput
python manage.py migrate

Your app's virtual environment lives under ~/virtualenv/. Check the folder for the exact path and Python version.

Install requirements

  1. Put requirements.txt in your code folder.
  2. In Developer, find your app under Node.js and Python apps and click Install packages.
  3. Click Restart.

Or in SSH: pip install -r requirements.txt.

Set environment variables

The Developer section doesn't have a screen for app variables yet. Keep values such as DJANGO_SECRET_KEY in a .env file outside your public folder, or open a request at https://barbrick.ca/submitticket.php and we'll set them for you, usually the same day. See Set environment variables for apps. Read them with os.environ["NAME"].

Database

MySQL: pip install mysqlclient or pymysql. See Create a MySQL database and user. PostgreSQL isn't offered; see Use PostgreSQL.

Restart

Click Restart in Developer after changes, or:

touch ~/apps/bakery/tmp/restart.txt

If something goes wrong

Problem Fix
"Incomplete response received from application" Read the log. Often an import error
ModuleNotFoundError Install requirements in the app's virtualenv, not system Python
Static files missing Run collectstatic, and confirm STATIC_ROOT is web-accessible
DisallowedHost Add the domain to ALLOWED_HOSTS
Slow first request The app starts on demand after idle

Was this helpful?

Still stuck? Ask us and we'll walk you through it.