Pyinstaller and Django Rest



我正在尝试将 Pyinstaller 与 django rest 一起使用,它可以很好地生成.exe,但在执行.exe时出现错误,错误是这样的

ModuleNotFoundError: No module named 'rest_framework'

我的问题是如何使用 Pyinstaller 安装依赖项,或者有没有其他方法可以做到这一点。

当代码中有动态导入时,会发生此错误。在这种情况下,pyinstaller 不会在 exe 文件中包含这些包。在这种情况下,您可以:

  1. 在代码中添加这些包的未使用导入
  2. 告诉 pyinstaller 包含它

一个文件选项不会更改运行代码时的任何内容。如果您正在创建 --onefile exe,则 pyinstaller 创建的所有文件都会打包到 exe 文件中,并在每次运行 exe 时解压缩到本地临时

。其他可能的解决方案包括:

解决方案 1: 从父目录运行您的命令,即而不是

c:compilationGui>pyinstaller --name=gui manage.py

c:compilation>pyinstaller --name=gui Guimanage.py

同时将运行服务器添加到文件末尾。

如果问题仍然存在,则 解决方案 2: pyinstaller --name=gui --exclude-module=PyQt4 --exclude-module=matplotlib --clean --win-private-assemblies manage.py runserver

确保正确安装了所需的模块。 安装所需的模块后,为 rest_framework 创建钩子并将其添加到 ..\site-packages\PyInstaller\hooks并将该文件命名为hook-rest_framework.py。文件内容:

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

这为我解决了同样的问题。

在你的manage.spec文件中,你需要在下面添加这样的代码:

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

您还需要添加另一个依赖项包

hiddenimports.extend(
[
'django.contrib.contenttypes',
'django.contrib.staticfiles',
'django_filters',
....
]
)

如果你的休息模板显示不存在,所以你需要将静态文件配置为吹;

datas=[(r'/env/lib/python3.6/site-packages/rest_framework/', './rest_framework'),               
(r'/env/lib/python3.6/site-packages/django_filters/', './django_filters')
]

在您的终端中:

pip install djangorestframework
pip install markdown
pip install django-filter 

python3 可以使用 pip3 install。

最新更新