gpt4 book ai didi

python - Django 项目的 Pyinstaller 错误 "ImportError: No module named ' django.contrib.admin.apps'”

转载 作者:太空狗 更新时间:2023-10-30 00:13:15 25 4
gpt4 key购买 nike

我在 django 中创建了一个项目。我已经使用 pyinstaller 为它创建了安装程序。如果我使用 python manage.py runserver 运行项目,那么项目运行正常,没有任何错误,但我不是能够通过安装程序运行它。运行安装程序时出现错误。

C:\The_Incredibles\Pyinstaller Story\test_proj>.\dist\demo\demo.exe runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper
at 0x03A15978>
Traceback (most recent call last):
File "site-packages\django-1.10.4-py3.5.egg\django\utils\autoreload.py", line
226, in wrapper
File "site-packages\django-1.10.4-py3.5.egg\django\core\management\commands\ru
nserver.py", line 113, in inner_run
File "site-packages\django-1.10.4-py3.5.egg\django\utils\autoreload.py", line
249, in raise_last_exception
File "site-packages\django-1.10.4-py3.5.egg\django\utils\six.py", line 685, in
reraise
File "site-packages\django-1.10.4-py3.5.egg\django\utils\autoreload.py", line
226, in wrapper
File "site-packages\django-1.10.4-py3.5.egg\django\__init__.py", line 27, in s
etup
File "site-packages\django-1.10.4-py3.5.egg\django\apps\registry.py", line 85,
in populate
File "site-packages\django-1.10.4-py3.5.egg\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'

演示.spec

# -*- mode: python -*-

block_cipher = None


a = Analysis(['test1\\manage.py'],
pathex=['C:\\The_Incredibles\\Pyinstaller Story\\test_proj'],
binaries=None,
datas=None,
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='demo',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='demo')

这是 PyInstaller 日志

(Sample) C:\The_Incredibles\Pyinstaller Story\test_proj>pyinstaller --name=demo
test1/manage.py
155 INFO: PyInstaller: 3.2
155 INFO: Python: 3.5.2
155 INFO: Platform: Windows-7-6.1.7601-SP1
157 INFO: wrote C:\The_Incredibles\Pyinstaller Story\test_proj\demo.spec
159 INFO: UPX is not available.
168 INFO: Extending PYTHONPATH with paths
['C:\\The_Incredibles\\Pyinstaller Story\\test_proj\\test1',
'C:\\The_Incredibles\\Pyinstaller Story\\test_proj']
168 INFO: checking Analysis
169 INFO: Building Analysis because out00-Analysis.toc is non existent
169 INFO: Initializing module dependency graph...
172 INFO: Initializing module graph hooks...
175 INFO: Analyzing base_library.zip ...
2829 INFO: running Analysis out00-Analysis.toc
2943 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-math-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3431 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-runtime-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3438 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-locale-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3445 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-heap-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3472 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-stdio-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3481 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-process-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3506 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-string-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3519 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-time-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3525 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-environment-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3545 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-convert-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3552 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-conio-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3564 WARNING: Can not get binary dependencies for file: C:\windows\system32\api-
ms-win-crt-filesystem-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
3567 INFO: Caching module hooks...
3572 INFO: Analyzing C:\The_Incredibles\Pyinstaller Story\test_proj\test1\manage
.py
6478 INFO: Loading module hooks...
6478 INFO: Loading module hook "hook-django.core.management.py"...
7684 INFO: Import to be excluded not found: 'matplotlib'
7685 INFO: Import to be excluded not found: 'tkinter'
7685 INFO: Excluding import 'IPython'
7688 WARNING: Removing import django.core.management.commands.shell from modul
e IPython
7689 WARNING: Removing import django.core.management.commands.shell from modul
e IPython.start_ipython
7689 WARNING: Removing import django.core.management.commands.shell from modul
e IPython.IPShell
7691 INFO: Loading module hook "hook-xml.py"...
7693 INFO: Loading module hook "hook-django.py"...
7694 INFO: Django root directory C:\The_Incredibles\Pyinstaller Story\test_proj\
test1\test1
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\utils\hooks\subpr
oc\django_import_finder.py", line 37, in <module>
list(settings.TEMPLATE_LOADERS) + \
File "c:\python\scripts\sample\lib\site-packages\django-1.10.4-py3.5.egg\djang
o\conf\__init__.py", line 54, in __getattr__
return getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'TEMPLATE_CONTEXT_PROCESSORS'

8910 INFO: Collecting Django migration scripts.
9818 INFO: Loading module hook "hook-django.core.mail.py"...
9880 INFO: Loading module hook "hook-encodings.py"...
9891 INFO: Loading module hook "hook-django.core.cache.py"...
9916 INFO: Loading module hook "hook-django.db.backends.py"...
10392 WARNING: Hidden import "django.db.backends.__pycache__.base" not found!
10393 INFO: Loading module hook "hook-pydoc.py"...
10395 INFO: Loading module hook "hook-xml.dom.domreg.py"...
10396 INFO: Loading module hook "hook-django.db.backends.oracle.base.py"...
10400 INFO: Loading module hook "hook-django.db.backends.mysql.base.py"...
10403 INFO: Loading module hook "hook-sqlite3.py"...
10478 INFO: checking Tree
10478 INFO: Building Tree because out00-Tree.toc is non existent
10479 INFO: Building Tree out00-Tree.toc
10969 INFO: Looking for ctypes DLLs
10986 INFO: Analyzing run-time hooks ...
10996 INFO: Including run-time hook 'pyi_rth_django.py'
11017 INFO: Looking for dynamic libraries
12997 WARNING: Can not get binary dependencies for file: C:\windows\system32\api
-ms-win-crt-utility-l1-1-0.dll
Traceback (most recent call last):
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 695, in getImports
return _getImports_pe(pth)
File "c:\python\scripts\sample\lib\site-packages\PyInstaller\depend\bindepend.
py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
12998 INFO: Looking for eggs
12999 INFO: Using Python library c:\python\scripts\sample\scripts\python35.dll
12999 INFO: Found binding redirects:
[]
13018 INFO: Warnings written to C:\The_Incredibles\Pyinstaller Story\test_proj\b
uild\demo\warndemo.txt
13157 INFO: checking PYZ
13158 INFO: Building PYZ because out00-PYZ.toc is non existent
13158 INFO: Building PYZ (ZlibArchive) C:\The_Incredibles\Pyinstaller Story\test
_proj\build\demo\out00-PYZ.pyz
14732 INFO: checking PKG
14732 INFO: Building PKG because out00-PKG.toc is non existent
14733 INFO: Building PKG (CArchive) out00-PKG.pkg
14784 INFO: Bootloader c:\python\scripts\sample\lib\site-packages\PyInstaller\bo
otloader\Windows-32bit\run.exe
14785 INFO: checking EXE
14785 INFO: Building EXE because out00-EXE.toc is non existent
14785 INFO: Building EXE from out00-EXE.toc
14786 INFO: Appending archive to EXE C:\The_Incredibles\Pyinstaller Story\test_p
roj\build\demo\demo.exe
14807 INFO: checking COLLECT
14808 INFO: Building COLLECT because out00-COLLECT.toc is non existent
14808 INFO: Building COLLECT out00-COLLECT.toc

(Sample) C:\The_Incredibles\Pyinstaller Story\test_proj>

最佳答案

我的问题得到解决。问题是 pyinstaller 无法导入所有隐藏的导入。我在 Lib\site-packages\PyInstaller\hooks 下添加了一个钩子(Hook)文件(hook-django.contrib.py)。

在钩子(Hook)文件中添加了以下代码

#-----------------------------------------------------------------------------
# Copyright (c) 2005-2016, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('django.contrib')

这对我有用。我已经使用 pyinstaller 从我的 djnago 项目创建了 exe 文件,我可以使用 exe 运行我的项目。

运行exe文件后的输出

(DemoEnv) C:\The_Incredibles\Pyinstaller Story\test_project>dist\manage\manage.e
xe runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you
apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
December 29, 2016 - 10:45:08
Django version 1.10.4, using settings 'demo.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[29/Dec/2016 10:45:26] "GET / HTTP/1.1" 200 1767

关于python - Django 项目的 Pyinstaller 错误 "ImportError: No module named ' django.contrib.admin.apps'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41342498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com