Pyinstaller - Django with custom admin commands

☆樱花仙子☆ 提交于 2019-12-24 18:47:35

问题


I'm trying to compile Django (1.8) app which has 2 custom commands on windows 7. We use pandas and other sci libs so we run the application via anaconda3.

When we use the Pyinstaller (version 3.0, add to the anaconda3 via anaconda pip script):

c:\Anaconda3\Scripts\pyinstaller.exe --name=compileTest  --exclude-module=PyQt4 --exclude-module=matplotlib  manage.py

We get a Django executable project but without the custom commands.

Can someone advice?


回答1:


This worked for me by including / overriding the django runtime hook.

https://pythonhosted.org/PyInstaller/#changing-runtime-behavior

Add --runtime-hook=pyi_rth_django.py to your PyInstaller command.

pyi_rth_django.py

Notice the omnibusd command that was added.

# This Django rthook was tested with Django 1.8.3.


import django.core.management
import django.utils.autoreload


def _get_commands():
    # Django groupss commands by app.
    # This returns static dict() as it is for django 1.8 and the default project.
    commands = {
         'runserver': 'django.core',
         'shell': 'django.core',
         'startapp': 'django.core',
         'startproject': 'django.core',
         'test': 'django.core',
         'testserver': 'django.core',
         'validate': 'django.core',
         'omnibusd': 'omnibus'
    }
    return commands


_old_restart_with_reloader = django.utils.autoreload.restart_with_reloader


def _restart_with_reloader(*args):
    import sys
    a0 = sys.argv.pop(0)
    try:
        return _old_restart_with_reloader(*args)
    finally:
        sys.argv.insert(0, a0)


# Override get_commands() function otherwise the app will complain that
# there are no commands.
django.core.management.get_commands = _get_commands
# Override restart_with_reloader() function otherwise the app might
# complain that some commands do not exist. e.g. runserver.
django.utils.autoreload.restart_with_reloader = _restart_with_reloader


来源:https://stackoverflow.com/questions/34479049/pyinstaller-django-with-custom-admin-commands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!