我想冻结一个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版。