目的
在使用pyinstaller打包python程序时一直出问题,记录下以便后续查看。
打包过程
- 首先应该创建一个单独的python环境,以免打包后的exe包含了多余的包,导致exe文件过大。
pyi-makespec.exe test.py
创建 test.spec 文件。spec文件官方说明:https://pyinstaller.org/en/stable/spec-files.html- 修改test.spec文件。
pyinstaller.exe test.spec
进行最后的打包。
出现的问题
- 运行打包后的exe,报错:找不到包;
- 添加自己写的包的搜索路径;
- 修改打包后的程序和文件夹名字;
block_cipher = None
a = Analysis(
['record_system_v28\\2record_system_v28.py'],
pathex=['D:\PycharmProjects\HPI'], # 当自己写的包找不到时,将包的搜索路径填入这里
binaries=[],
datas=[],
hiddenimports=['qimage2ndarray','pyqtgraph','osgeo','configparser','socket_motor'], # 当运行打包后的程序报找不到包的错误时,将包加入这里
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='2record_system_v28', # 打包后的exe名
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='2record_system_v28', # 打包后的文件夹名
)
- 自己写了一个影像读写包:基于gdal封装了一个类,但是打包后运行exe报错莫名其妙,最后通过把这个类放到主文件中解决。