Skip to content

Deploying

Deploy a Python app (Django, FastAPI, Flask)

How DployHub builds and runs Python apps on Python 3.12: dependency install, Django, FastAPI and Flask detection, gunicorn, uvicorn and port 8000.

Updated 2 min read

A requirements.txt, pyproject.toml or Pipfile at the root folder tells DployHub the project is Python. We build on python:3.12-slim, install your dependencies, detect Django, FastAPI or Flask and start the app on port 8000 with a production server. You can override every detected value in Settings.

Installing dependencies

  • requirements.txt: pip install -r requirements.txt.
  • pyproject.toml: pip install ..
  • Pipfile: installed with pipenv.

If the start command uses gunicorn or uvicorn, we install gunicorn or uvicorn[standard] for you, so you do not have to add them to your requirements.

Django

Django is detected when django is in your requirements or a manage.py exists. We find the folder containing wsgi.py and run:

bash
# build
python manage.py collectstatic --noinput || true
# start
gunicorn mysite.wsgi --bind 0.0.0.0:8000

FastAPI

We look for a file containing FastAPI( among main.py, app.py, app/main.py, src/main.py and api/main.py, and build the module path from it. For app/main.py the start command is uvicorn app.main:app --host 0.0.0.0 --port 8000. The application variable must be named app:

python
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def health():
    return {"status": "ok"}

Flask and other Python apps

Flask is detected from Flask( in app.py, wsgi.py, main.py, application.py or app/__init__.py, and started with gunicorn app:app --bind 0.0.0.0:8000 (module name adjusted to the file found). Any other Python project starts with python main.py, app.py or server.py, whichever exists. In that case make sure your code listens on 0.0.0.0 and on the PORT environment variable, which we always set.

Files, databases and secrets

  • Store uploads and SQLite files under /data, the persistent storage mount. Everything else is reset on each deploy. See Persistent storage.
  • Put SECRET_KEY, DATABASE_URL and similar values in environment variables, separately for staging and live.
  • For critical data, use a managed database rather than a file on disk.

Overrides and troubleshooting

Choose Python under How to build it to skip detection, or edit the start command, build command and port in Settings at any time. If the release fails with a message about port 8000, check that your start command binds to 0.0.0.0:8000 or uses $PORT. Framework quick guides live at /deploy/django, /deploy/fastapi and /deploy/flask. If you are still stuck, raise a ticket.

Frequently asked questions

Which Python version is used?

Detected Python apps use python:3.12-slim. For another version, deploy with your own Dockerfile.

Do I need gunicorn or uvicorn in requirements.txt?

No. If your start command uses them we install gunicorn or uvicorn[standard] automatically.

Why does Django return 400 Bad Request?

Your app address is not in ALLOWED_HOSTS. Add the live, staging and custom domain hostnames and redeploy.