优化exe文件体积
This commit is contained in:
+120
@@ -0,0 +1,120 @@
|
||||
# -*- mode: python ; coding: utf-8 -*-
|
||||
"""PyInstaller spec: 将 main.py 打包为体积尽量小的单文件 exe。
|
||||
体积优化手段:
|
||||
1) onefile (EXE 内嵌 binaries/datas)
|
||||
2) 排除未使用的大型第三方库与 PyQt5 子模块
|
||||
(注意: 不排除任何标准库模块, 避免启动 rthook 阶段导入链断裂)
|
||||
3) 过滤 a.binaries, 仅保留 Qt5Core/Qt5Gui/Qt5Widgets 三个必需 DLL
|
||||
4) UPX 压缩 (运行打包时需把 upx 目录加入 PATH 或用 --upx-dir)
|
||||
5) windowed (无控制台)
|
||||
"""
|
||||
import re
|
||||
|
||||
# 排除的模块: 仅排除本程序未用到的第三方库与 PyQt5 子模块
|
||||
# 警告: 切勿把 Python 标准库模块(http/urllib/xml/html/email 等) 放入此列表,
|
||||
# 否则 PyInstaller rthook 在启动阶段将因依赖链断裂而崩溃
|
||||
# (如 ModuleNotFoundError: No module named 'urllib')
|
||||
excluded_modules = [
|
||||
# 大型第三方库 (环境中存在但本程序未使用)
|
||||
'scipy', 'pandas', 'matplotlib', 'PIL', 'cv2', 'opencv', 'sklearn',
|
||||
'sympy', 'tables', 'lxml', 'sqlalchemy', 'QCustomPlot_PyQt5',
|
||||
'pyqt5_tools', 'pyqt5_plugins', 'h5py',
|
||||
# PyQt5 中未使用的大模块 (这些是 PyQt5 的子模块, 非标准库)
|
||||
'PyQt5.QtWebEngineCore', 'PyQt5.QtWebEngineWidgets',
|
||||
'PyQt5.QtWebChannel', 'PyQt5.QtWebSockets',
|
||||
'PyQt5.QtMultimedia', 'PyQt5.QtMultimediaWidgets',
|
||||
'PyQt5.QtNetwork', 'PyQt5.QtSql', 'PyQt5.QtTest',
|
||||
'PyQt5.QtSvg', 'PyQt5.QtSvgWidgets',
|
||||
'PyQt5.QtOpenGL', 'PyQt5.QtBluetooth',
|
||||
'PyQt5.QtSerialPort', 'PyQt5.QtSensors', 'PyQt5.QtPositioning',
|
||||
'PyQt5.QtNfc', 'PyQt5.QtLocation', 'PyQt5.QtQuick', 'PyQt5.QtQml',
|
||||
'PyQt5.QtQuickWidgets', 'PyQt5.QtDesigner', 'PyQt5.QtHelp',
|
||||
'PyQt5.QtChart', 'PyQt5.QtDataVisualization',
|
||||
'PyQt5.Qt3DCore', 'PyQt5.Qt3DRender', 'PyQt5.Qt3DInput',
|
||||
'PyQt5.Qt3DLogic', 'PyQt5.Qt3DAnimation', 'PyQt5.Qt3DExtras',
|
||||
'PyQt5.QtAxContainer', 'PyQt5.QtDBus', 'PyQt5.QtXml',
|
||||
'PyQt5.QtPrintSupport', 'PyQt5.QtTextToSpeech',
|
||||
'PyQt5.QtRemoteObjects', 'PyQt5.QtScript', 'PyQt5.QtScriptTools',
|
||||
'PyQt5.QtUiTools',
|
||||
# 同样是第三方工具链库, 本程序未使用
|
||||
'setuptools', 'pip', 'ensurepip', 'wheel',
|
||||
]
|
||||
|
||||
a = Analysis(
|
||||
['main.py'],
|
||||
pathex=[],
|
||||
binaries=[],
|
||||
datas=[],
|
||||
hiddenimports=[],
|
||||
hookspath=[],
|
||||
hooksconfig={},
|
||||
runtime_hooks=[],
|
||||
excludes=excluded_modules,
|
||||
noarchive=False,
|
||||
)
|
||||
|
||||
|
||||
def _is_unneeded_binary(name: str) -> bool:
|
||||
"""过滤掉 PyQt5 钩子无条件收集、但本程序用不到的 Qt5 DLL / 插件 / qml 模块。
|
||||
仅保留 Qt5Core/Qt5Gui/Qt5Widgets 以及 qwindows 平台插件等基础组件。
|
||||
"""
|
||||
n = name.replace('\\', '/').lower()
|
||||
# Qt5*.dll 中除 Core/Gui/Widgets 外全部移除
|
||||
m = re.search(r'qt5([a-z0-9]+)\.dll$', n)
|
||||
if m and m.group(1) not in ('core', 'gui', 'widgets'):
|
||||
return True
|
||||
# QML / Quick 运行时目录(被 Qt5Quick 拖入, 本程序不用)
|
||||
if '/qml/' in n or '/qtqml/' in n or '/qmltypes' in n or '/plugins/qml/' in n:
|
||||
return True
|
||||
# 不使用 OpenGL: 移除软件渲染器 / ANGLE / HLSL 编译器
|
||||
if n.endswith(('opengl32sw.dll', 'd3dcompiler_47.dll', 'libegl.dll', 'libglesv2.dll')):
|
||||
return True
|
||||
# 平台插件只需 qwindows
|
||||
if '/plugins/platforms/' in n and not n.endswith('qwindows.dll'):
|
||||
return True
|
||||
# Linux 桌面门户主题插件, Windows 不需要
|
||||
if '/plugins/platformthemes/' in n:
|
||||
return True
|
||||
# 未使用的 Qt 插件子目录
|
||||
for bad in ('/plugins/mediaservice/', '/plugins/audio/', '/plugins/sqldrivers/',
|
||||
'/plugins/sensorgestures/', '/plugins/sensors/', '/plugins/position/',
|
||||
'/plugins/geoservices/', '/plugins/printsupport/',
|
||||
'/plugins/scenegraph/', '/plugins/sceneparsers/',
|
||||
'/plugins/platforminputcontexts/', '/plugins/canbus/',
|
||||
'/plugins/playlistformats/', '/plugins/bearer/',
|
||||
'/plugins/gamepads/', '/plugins/tls/',
|
||||
'/plugins/iconengines/', '/plugins/generic/'):
|
||||
if bad in n:
|
||||
return True
|
||||
# 不常用的图片格式插件(保留 gif/ico/jpeg/svg)
|
||||
if '/plugins/imageformats/' in n and n.endswith(
|
||||
('qicns.dll', 'qtga.dll', 'qtiff.dll', 'qwbmp.dll', 'qwebp.dll')):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
a.binaries = [b for b in a.binaries if not _is_unneeded_binary(b[0])]
|
||||
a.datas = [d for d in a.datas if not _is_unneeded_binary(d[0])]
|
||||
|
||||
pyz = PYZ(a.pure)
|
||||
|
||||
exe = EXE(
|
||||
pyz,
|
||||
a.scripts,
|
||||
a.binaries,
|
||||
a.zipfiles,
|
||||
a.datas,
|
||||
[],
|
||||
name='SikoraDaqReader',
|
||||
debug=False,
|
||||
bootloader_ignore_signals=False,
|
||||
strip=False,
|
||||
upx=True,
|
||||
runtime_tmpdir=None,
|
||||
console=False,
|
||||
disable_windowed_traceback=False,
|
||||
target_arch=None,
|
||||
codesign_identity=None,
|
||||
entitlements_file=None,
|
||||
icon=None,
|
||||
)
|
||||
Reference in New Issue
Block a user