Created
December 3, 2018 13:53
-
-
Save leonh/ad5ffc6bdfa4e5c7a165300cdb146233 to your computer and use it in GitHub Desktop.
Django application (Wagtail CMS Bakery demo) as Python zipapp using Shiv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| I ran into a couple of problems following the example on the Shiv (RTD) docs page | |
| The following files worked for me from a python3 virtual env called test_shiv_env | |
| Created .py file | |
| test_shiv_env/bakerydemo/bakerydemo/main.py | |
| import sys | |
| import django | |
| def main(): | |
| # setup django | |
| os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bakerydemo.settings.dev") | |
| django.setup() | |
| try: | |
| production = sys.argv[1] == "production" | |
| except IndexError: | |
| production = False | |
| if production: | |
| import gunicorn.app.wsgiapp as wsgi | |
| # This is just a simple way to supply args to gunicorn | |
| sys.argv = [".", "bakerydemo.wsgi", "--bind=127.0.0.1:8000"] | |
| wsgi.run() | |
| else: | |
| from django.core.management import call_command | |
| call_command("runserver") | |
| Then I created build.sh file | |
| test_shiv_env/bakerydemo/build2.sh | |
| #!/usr/bin/env bash | |
| project_name=bakerydemo | |
| # 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 manage.py dist | |
| cp -r ${project_name}db dist | |
| cp -r ${project_name} dist | |
| # finally, build! | |
| shiv --site-packages dist --compressed -p '/usr/bin/env python3' -o ${project_name}.pyz -e ${project_name}.main:main | |
| Make sure you have the wsgi.py | |
| test_shiv_env/bakerydemo/bakerydemo/wsgi.py | |
| """ | |
| WSGI config for portal project. | |
| It exposes the WSGI callable as a module-level variable named ``application``. | |
| For more information on this file, see | |
| https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ | |
| """ | |
| import os | |
| import dotenv | |
| from django.core.wsgi import get_wsgi_application | |
| dotenv.read_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env')) | |
| os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bakerydemo.settings.dev") | |
| application = get_wsgi_application() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment