Pyinstaller - 具有自定义管理命令的Django [英] Pyinstaller - Django with custom admin commands

查看:498
本文介绍了Pyinstaller - 具有自定义管理命令的Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译在Windows 7上有2个自定义命令的Django(1.8)应用程序。
我们使用大熊猫和其他sci库,以便我们通过anaconda3运行应用程序。



当我们使用Pyinstaller(3.0版,通过anaconda pip脚本添加到anaconda3):

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

我们得到一个Django可执行项目,但没有自定义命令。



有人可以咨询吗?

解决方案

这可以通过包含/覆盖django运行时钩子来实现。



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



- runtime-hook = pyi_rth_django.py 添加到您的PyInstaller命令中。



pyi_rth_django.py



注意 omnibusd 该命令被添加。

 #这个D​​jango rthook是用Django 1.8.3测试的。 


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


def _get_commands():
#由应用程序Django groupss命令
#这将返回静态dict(),就像django 1.8和默认项目一样。
commands = {
'runserver':'django.core',
'shell':'django.core',
'startapp':'django.core',
'startproject':'django.core',
'test':'django.core',
'testserver':'django.core',
'validate' django.core',
'omnibusd':'omnibus'
}
返回命令


_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)


#覆盖get_commands()函数,否则应用程序会抱怨
#没有命令。
django.core.management.get_commands = _get_commands
#覆盖restart_with_reloader()函数,否则应用程序可能
#抱怨一些命令不存在。例如runserver命令。
django.utils.autoreload.restart_with_reloader = _restart_with_reloader


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?

解决方案

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

这篇关于Pyinstaller - 具有自定义管理命令的Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆