Deploying django apps

Because of how shiv works, you can ship entire django apps with shiv, even including the database if you want!

Defining an entrypoint

First, we will need an entrypoint.

We’ll call it, and store it at <project_name>/<project_name>/ (alongside

import os
import sys

import django

# setup django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<project_name>.settings")

    production = sys.argv[1] == "production"
except IndexError:
    production = False

if production:
    import as wsgi

    # This is just a simple way to supply args to gunicorn
    sys.argv = [".", "<project_name>.wsgi", "--bind="]
    from import call_command


This is meant as an example. While it’s fully production-ready, you might want to tweak it according to your project’s needs.

Build script

Next, we’ll create a simple bash script that will build a zipapp for us.

Save it as (next to

#!/usr/bin/env bash

# clean old build
rm -r dist <project_name>.pyz

# include the dependencies from `pip freeze`
pip install -r <(pip freeze) --target dist/

# or, if you're using pipenv
# pip install -r  <(pipenv lock -r) --target dist/

# specify which files to be included in the build
# You probably want to specify what goes here
cp -r \
-t dist \
<app1> <app2> db.sqlite3

# finally, build!
shiv --site-packages dist --compressed -p '/usr/bin/env python3' -o <project_name>.pyz -e <project_name>.main

And then, you can just do the following

$ ./

$ ./<project_name>.pyz

# In production -

$ ./<project_name>.pyz production