OpenCore Legacy Patcher终极指南:让老旧Mac重获新生的专业解决方案
2026/7/6 14:14:48
在上一篇我们系统梳理了 Python 打包工具的历史与对比,结论是PyInstaller仍是桌面应用和现场部署的首选。本篇将专注于PyInstaller 5.13.2——这是兼顾稳定性与兼容性的版本,尤其在 Windows 7 + Python 3.7 环境下表现可靠。我们将从安装、打包模式、常用参数,到spec 文件的深度解析,全面覆盖,让你能从入门到精通。
pipinstallpyinstaller==5.13.2最佳实践:开发调试阶段用单目录,发布阶段用单文件。
--onefile:单文件打包--windowed:去掉控制台窗口(适合 GUI 程序)--icon=xxx.ico:自定义图标--add-data "data;data":添加数据文件(Windows 下用分号分隔)--hidden-import:解决依赖未自动识别问题--version-file version.txt:嵌入版本信息相比命令行,spec 文件提供了更灵活的配置方式,适合复杂项目。打包时可直接运行:
pyinstaller myapp.spec# -*- mode: python ; coding: utf-8 -*-block_cipher=Nonea=Analysis(['pelco_tool_v1.py'],pathex=[],binaries=[],datas=[],hiddenimports=[],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,a.binaries,a.zipfiles,a.datas,[],name='pelco_tool_v1',debug=False,bootloader_ignore_signals=False,strip=False,upx=True,upx_exclude=[],runtime_tmpdir=None,console=True,disable_windowed_traceback=False,argv_emulation=False,target_arch=None,codesign_identity=None,entitlements_file=None,)datas用于添加额外资源(如配置文件、数据库)添加 DLL 或二进制文件
binaries=[('dlls/mylib.dll','.')]嵌入版本信息
version='version.txt'多入口脚本
在a = Analysis([...])中添加多个 py 文件
PyInstaller 支持通过文本文件定义版本信息,通常命名为version.txt。内容格式类似于 Windows 的资源脚本:
# UTF-8 # PyInstaller 6.x version.txt 示例 VSVersionInfo( ffi=FixedFileInfo( filevers=(1, 0, 0, 0), # 文件版本号 prodvers=(1, 0, 0, 0), # 产品版本号 mask=0x3f, flags=0x0, OS=0x4, # Windows NT fileType=0x1, # 应用程序 subtype=0x0, date=(0,0) ), kids=[ StringFileInfo([ StringTable( '080404b0', # 简体中文 [ StringStruct(u'CompanyName', u'智码电子'), StringStruct(u'FileDescription', u'Pelco-D/P 万能现场维护工具'), StringStruct(u'FileVersion', u'1.0.0.0'), StringStruct(u'InternalName', u'pelco_tool_v1'), StringStruct(u'LegalCopyright', u'Copyright © 2025 我送炭你添花'), StringStruct(u'OriginalFilename', u'pelco_tool_v1.exe'), StringStruct(u'ProductName', u'Pelco Tool'), StringStruct(u'ProductVersion', u'1.0.0.0') ] ) ]), VarFileInfo([VarStruct(u'Translation', [0x0804, 1200])]) ] )语言的设置主要通过StringTable的代码页和语言 ID来控制。关键点在于:
StringTable(u'040904B0', [...])0409表示语言 ID(这里是美国英语,LCID=0x0409)。04B0表示代码页(这里是 Unicode UTF-16)。VarFileInfo([VarStruct(u'Translation', [0x0409, 1200])])0x0409是语言 ID。1200是代码页(1200 = Unicode UTF-16)。| 语言 | LCID (十六进制) | 代码页 |
|---|---|---|
| 简体中文 | 0x0804 | 936(GBK) 或1200(Unicode) |
| 繁体中文 | 0x0404 | 950(Big5) 或1200 |
| 英文 (美国) | 0x0409 | 1252(ANSI Latin I) 或1200 |
| 日文 | 0x0411 | 932(Shift-JIS) 或1200 |
pyinstaller --onefile --console --icon=keyboard.ico --version-file=version.txt pelco_tool_v1.py这样生成的 exe 文件在 Windows 属性中就会显示上述信息。
如果使用.spec文件,可以在EXE部分添加:
# -*- mode: python ; coding: utf-8 -*-block_cipher=Nonea=Analysis(['pelco_tool_v1.py'],pathex=[],binaries=[],datas=[],hiddenimports=[],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,a.binaries,a.zipfiles,a.datas,[],name='pelco_tool_v1',debug=False,bootloader_ignore_signals=False,strip=False,upx=True,upx_exclude=[],runtime_tmpdir=None,console=True,disable_windowed_traceback=False,argv_emulation=False,target_arch=None,codesign_identity=None,entitlements_file=None,version='version.txt',icon='keyboard.ico',)打包命令:
pyinstaller --clean --noconfirm pelco_tool_v1.spec1033,1200表示英语 + Unicode。中文环境可用2052,1200。最佳实践:
FileVersion和ProductVersion。这样,你的PyInstaller 5.13.2 打包专篇就和上一篇形成了呼应:前一篇讲工具全景对比,这一篇不仅讲 PyInstaller 的打包,还深入到版本信息与 spec 文件配置,让读者能做出专业级的 exe。
binariesPyInstaller 5.13.2 是目前最稳定的版本之一,兼顾兼容性与功能。掌握其命令行参数与spec 文件配置,能让你在任何现场快速生成可靠的可执行工具。