wiki:Recipe/DjangoApplication

Build an executable from a Python application using Django

Since PyInstaller 2.1, you can build an executable from a project using Django 1.4. PyInstaller will take care of a lot of magic needed to correctly build Django applications. For instance, it will parse all your files and find all the dotted-names strings that refers to module names (eg: within settings.py) and make sure that all those modules are packaged.

You should be able to run PyInstaller over the manage.py script directly.

The following example should clarify the required steps.

Example

Let's say that your Django project is called mysite. If you used the django-admin.py wizard, you probably have ended up with a Django 1.4 project directory structure. PyInstaller expects directory structure like this:

./myproject_dir/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py
        ...

Use PyInstaller:

 $ pyinstallery --name=mysite myproject_dir/manage.py

Notice that I'm using the --name option when invoking pyinstaller to make sure I end up with a packaged executable called mysite (and not manage).

Now you should have your frozen django app in directory ./dist/mysite/. You should be able to see an executable file ./dist/mysite/mysite.exe. Use this file the same way as you would use manage.py.

To run the built-in Django test server run:

 $ ./dist/mysite/mysite.exe runserver localhost:8080

What if it does not work?

Django uses a lot of magic behind the hood, so some quirks are expected. If the application does not run, you should see the traceback in the console. If it is an ImportError, it is probably a missing module whose dependency PyInstaller was not able to automatically discover.

The quickest workaround is to explicitly import that module somewhere, for instance in the manage.py script. But please report the incident to the mailing list so that we can prepare a proper fix that everyone can benefit from.

If this does not help, have a look at HowtoReportBugs#Beforesubmittingareport:Makesureeverythingispackaged to track down the problem.

Last modified 12 months ago Last modified on Sep 25, 2013 2:30:16 PM