我想冻结一个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应该生成一个可以执行以生成inception.exe的可执行exemple.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,但它并没有真正的帮助。这有可能吗?如果是这样的话,是怎么做的?
这需要在装有Python 3.7的Windows10上运行。我使用的是PyInstaller 3.5版。
发布于 2020-03-03 18:53:51
从其中一个PyInstaller开发人员:
要做到这一点,你需要一个PyInstaller的钩子--我们没有,也不会创建一个。您还需要修改引导加载器进程,创建运行时挂钩,以及加载更多内容。我们不能,不会,也不会支持或鼓励这样做。
你不能用PyInstaller做到这一点,一种解决办法是使用另一种软件,如nuitka和Pyoxidizer或Embed your app manually。
https://stackoverflow.com/questions/58755392
复制相似问题