系统:Windows 7 64位,python 3.5,anaconda 3(64位),django 1.10.1
我试图以两种方式编译我的django项目:
首先:
[Anaconda3] c:\compilation\Gui>pyinstaller --name=gui --exclude-module=PyQt4 --exclude-module=matplotlib --clean --win-private-assemblies manage.py
[Anaconda3] c:\compilation\Gui>pyinstaller --name=gui --exclude-module=PyQt4 --exclude-module=matplotlib --clean --win-private-assemblies --runtime-hook=pyi_rth_django.py manage.py
当我尝试运行输出时:
c:\compilation\Gui\dist\gui>gui.exe runserver
我得到了(对于2个版本,我得到相同的输出):
c:\compilation\Gui\dist\gui>gui.exe runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000000044E9D90>
Traceback (most recent call last):
File "site-packages\django\utils\autoreload.py", line 226, in wrapper
File "site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
File "site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
File "site-packages\django\utils\six.py", line 685, in reraise
File "site-packages\django\utils\autoreload.py", line 226, in wrapper
File "site-packages\django\__init__.py", line 27, in setup
File "site-packages\django\apps\registry.py", line 85, in populate
File "site-packages\django\apps\config.py", line 116, in create
File "importlib\__init__.py", line 126, in import_module
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'django.contrib.admin.apps'
请建议。
答案 0 :(得分:1)
你的文件布局是什么?根据这些pyinstaller
文档https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Executable-From-Django,可能有两种解决方案。
从父目录运行命令,即代替
c:\compilation\Gui>pyinstaller --name=gui manage.py
DO
c:\compilation>pyinstaller --name=gui Gui\manage.py
尝试将import django.contrib.admin.apps
添加到manage.py
并确保其存在
答案 1 :(得分:1)
要修复“ImportError:没有名为django.contrib.admin.apps的模块”问题,您必须创建一个目录,我们称之为“your_project / other_hooks”。 在该目录中,使用以下内容创建名为hook-django.contrib.py的文件:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('django.contrib')
现在以这种方式调用pyinstaller:
pyinstaller --name=yourProject --additional-hooks-dir=your_project\other_hooks your_project\manage.py
虽然您可以为目录使用任何名称,但文件名是必需的,它必须是“hook-django.contrib.py”。
希望这有帮助。
参考文献:
https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Executable-From-Django https://github.com/pyinstaller/pyinstaller/issues/2332 https://pythonhosted.org/PyInstaller/hooks.html#how-a-hook-is-loaded