Django 项目的 Pyinstaller 错误“ImportError:没有名为 'django.contrib.admin.apps' 的模块”

2024-03-31

我在 django 中创建了一个项目。我已经使用 pyinstaller 为其创建了安装程序。如果我使用运行项目python 管理.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'

演示规范

# -*- 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-django.contrib.py)。

将以下代码添加到钩子文件中

#-----------------------------------------------------------------------------
# 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 从 django 项目创建了 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django 项目的 Pyinstaller 错误“ImportError:没有名为 'django.contrib.admin.apps' 的模块” 的相关文章

  • python future 和元组解包

    实现像使用 future 进行元组解包这样的事情的优雅 惯用的方法是什么 我有这样的代码 a b c f x y g a b z h y c 我想将其转换为使用期货 理想情况下我想写一些类似的东西 a b c ex submit f x y
  • 我怎样才能更多地了解Python的内部原理? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我使用Python编程已经有半年多了 我对Python内部更感兴趣 而不是使用Python开发应用程序
  • python 中的代表

    我实现了这个简短的示例来尝试演示一个简单的委托模式 我的问题是 这看起来我已经理解了委托吗 class Handler def init self parent None self parent parent def Handle self
  • Python模块可以访问英语词典,包括单词的定义[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找一个 python 模块 它可以帮助我从英语词典中获取单词的定义 当然有enchant 这可以帮助我检查该单词是否存在于英语中
  • python 模拟第三方模块

    我正在尝试测试一些处理推文的类 我使用 Sixohsix twitter 来处理 Twitter API 我有一个类充当 Twitter 类的外观 我的想法是模拟实际的 Sixohsix 类 通过随机生成新推文或从数据库检索它们来模拟推文的
  • 如何在 pytest 中将单元测试和集成测试分开

    根据维基百科 https en wikipedia org wiki Unit testing Description和各种articles https techbeacon com devops 6 best practices inte
  • 如何创建一个语句来打印以特定单词开头的单词? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 如何在 python 中打印从特定字母开始的单词 而不使用函数 而是使用方法或循环 1 我有一个字符串 想要打印以 m 开头的单词 S
  • Pandas 数据帧到 numpy 数组 [重复]

    这个问题在这里已经有答案了 我对 Python 很陌生 经验也很少 我已经设法通过复制 粘贴和替换我拥有的数据来使一些代码正常工作 但是我一直在寻找如何从数据框中选择数据 但无法理解这些示例并替换我自己的数据 总体目标 如果有人真的可以帮助
  • 切片 Dataframe 时出现 KeyError

    我的代码如下所示 d pd read csv Collector Output csv df pd DataFrame data d dfa df copy dfa dfa rename columns OBJECTID Object ID
  • 使用 OLS 回归预测未来值(Python、StatsModels、Pandas)

    我目前正在尝试在 Python 中实现 MLR 但不确定如何将我找到的系数应用于未来值 import pandas as pd import statsmodels formula api as sm import statsmodels
  • 如何通过在 Python 3.x 上按键来启动和中断循环

    我有这段代码 当按下 P 键时会中断循环 但除非我按下非 P 键 否则循环不会工作 def main openGame while True purchase imageGrab if a sum gt 1200 fleaButton ti
  • Numpy 过滤器平滑零区域

    我有一个 0 及更大整数的 2D numpy 数组 其中值代表区域标签 例如 array 9 9 9 0 0 0 0 1 1 1 9 9 9 9 0 7 1 1 1 1 9 9 9 9 0 2 2 1 1 1 9 9 9 8 0 2 2 1
  • 将 JSON 对象传递给带有请求的 url

    所以 我想利用 Kenneth 的优秀请求模块 https github com kennethreitz requests 在尝试使用时偶然发现了这个问题自由库API http wiki freebase com wiki API 基本上
  • Pandas 根据 diff 列形成簇

    我正在尝试使用 Pandas 根据表示时间 以秒为单位 的列中的差异来消除数据框中的一些接近重复项 例如 import pandas as pd numpy as np df pd DataFrame 1200 1201 1233 1555
  • 如何使用列表作为pandas数据框中的值?

    我有一个数据框 需要列的子集包含具有多个值的条目 下面是一个带有 运行时 列的数据框 其中包含程序在各种条件下的运行时 df condition a runtimes 1 1 5 2 condition b runtimes 0 5 0 7
  • 如何在 OSX 上安装 numpy 和 scipy?

    我是 Mac 新手 请耐心等待 我现在使用的是雪豹 10 6 4 我想安装numpy和scipy 所以我从他们的官方网站下载了python2 6 numpy和scipy dmg文件 但是 我在导入 numpy 时遇到问题 Library F
  • 如何读取Python字节码?

    我很难理解 Python 的字节码及其dis module import dis def func x 1 dis dis func 上述代码在解释器中输入时会产生以下输出 0 LOAD CONST 1 1 3 STORE FAST 0 x
  • 列表值的意外更改

    这是我的课 class variable object def init self name name alias parents values table name of the variable self name 这是有问题的函数 f
  • 迭代 pandas 数据框的最快方法?

    如何运行数据框并仅返回满足特定条件的行 必须在之前的行和列上测试此条件 例如 1 2 3 4 1 1 1999 4 2 4 5 1 2 1999 5 2 3 3 1 3 1999 5 2 3 8 1 4 1999 6 4 2 6 1 5 1
  • Scrapy Spider不存储状态(持久状态)

    您好 有一个基本的蜘蛛 可以运行以获取给定域上的所有链接 我想确保它保持其状态 以便它可以从离开的位置恢复 我已按照给定的网址进行操作http doc scrapy org en latest topics jobs html http d

随机推荐