如何在PyInstaller捆绑包中包含PyInstaller?

我想冻结一个Python应用程序,它具有使用PyInstaller生成冻结的Python应用程序作为其功能之一。这是一个最小的应用程序,显示了我想要实现的目标:

import PyInstaller.__main__

with open('inception','w',encoding='utf-8') as f:
    f.write('import sys; print("Hello from the inside")\n')
PyInstaller.__main__.run(['--noconfirm','--onedir','inception'])

使用PyInstaller冻结它

PS> pyinstaller --noconfirm --onedir example.py

应生成可执行文件exemple.exe,可执行文件可以生成inception.exe

第一次尝试,我遇到了以下错误

PS> .\dist\example\example.exe
PyInstaller cannot check for assembly dependencies.
Please install pywin32-ctypes.

pip install pywin32-ctypes

此问题已通过安装pywin32(已安装pywin32-ctypes)并按here所述更改PyInstaller的compat.py文件来解决。现在重新捆绑应用程序会导致以下错误

PS> .\dist\example\example.exe
Traceback (most recent call last):
  File "example.py",line 2,in <module>
    import PyInstaller.__main__
  File "<frozen importlib._bootstrap>",line 983,in _find_and_load
  File "<frozen importlib._bootstrap>",line 967,in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>",line 677,in _load_unlocked
  File "d:\code\stackoverflow\pyinstaller_inception\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",line 627,in exec_module
    exec(bytecode,module.__dict__)
  File "lib\site-packages\PyInstaller\__init__.py",line 66,in <module>
  File "lib\site-packages\pkg_resources\__init__.py",line 481,in get_distribution
  File "lib\site-packages\pkg_resources\__init__.py",line 357,in get_provider
  File "lib\site-packages\pkg_resources\__init__.py",line 900,in require
  File "lib\site-packages\pkg_resources\__init__.py",line 786,in resolve
pkg_resources.DistributionNotFound: The 'PyInstaller' distribution was not found and is required by the application
[14376] Failed to execute script example

因此,似乎PyInstaller并未将其自身捆绑在应用程序内部。 PyInstaller github页面上有一个issue,但这并没有真正的帮助。这有可能吗?如果可以,怎么办?

这需要在Windows 10和Python 3.7上运行。我正在使用PyInstaller 3.5版。

CL1314520CLCL 回答:如何在PyInstaller捆绑包中包含PyInstaller?

尝试在命令中使用“ --hidden-import =“标志。

您也可以查看文档以获取更多信息here

,

从其中一个PyInstaller开发人员那里:

要执行此操作,您需要为PyInstaller挂上钩子-我们没有钩子,也不会创建钩子。您还需要修改引导加载程序进程,创建运行时挂钩并加载更多内容。我们不能,不会,也不支持或鼓励这一点。

TL; DR:您不能使用PyInstaller来做到这一点,一种解决方法是使用另一软件,例如nuitkaPyoxidizerEmbed your app manually

本文链接:https://www.f2er.com/3142226.html

大家都在问