PyInstaller问题集合
一、打包成应用程序
打包命令:
pyinstaller --onefile --windowed --add-data="/Users/01395756/Documents/pythonScripts/venv_39/lib/python3.9/site-packages:site-packages" --exclude-module=rapidfuzz.__pyinstaller main.py
日志如下
288 DEPRECATION: Onefile mode in combination with macOS .app bundles (windowed mode) don't make sense (a .app bundle can not be a single file) and clashes with macOS's security. Please migrate to onedir mode. This will become an error in v7.0.
289 INFO: wrote /Users/01395756/Documents/pythonScripts/get_mac_sysinfo/main.spec
299 DEPRECATION: Foreign Python environment's site-packages paths added to --paths/pathex:
['/Users/01395756/Documents/pythonScripts/venv_39/lib/python3.9/site-packages']
This is ALWAYS the wrong thing to do. If your environment's site-packages is not in PyInstaller's module search path then you are running PyInstaller from a different environment to the one your packages are in. Run print(sys.prefix) without PyInstaller to get the environment you should be using then install and run PyInstaller from that environment instead of this one. This warning will become an error in PyInstaller 7.0.
299 INFO: Module search paths (PYTHONPATH):
['/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip',
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9',
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload',
'/Users/01395756/Documents/pythonScripts/venv_39/lib/python3.9/site-packages',
'/Users/01395756/Documents/pythonScripts/get_mac_sysinfo',
'/Users/01395756/Documents/pythonScripts/venv_39/lib/python3.9/site-packages']
167 WARNING: discover_hook_directories: Failed to process hook entry point 'EntryPoint(name='hook-dirs', value='rapidfuzz.__pyinstaller:get_hook_dirs', group='pyinstaller40')': AttributeError: module 'rapidfuzz.__pyinstaller' has no attribute 'get_hook_dirs'
根据你的日志信息,问题主要出在 PyInstaller 的打包方式与环境配置 上。以下是关键问题的分析和解决方案:
1. 核心问题:--onefile
+ --windowed
在 macOS 上的冲突
错误信息
DEPRECATION: Onefile mode in combination with macOS .app bundles (windowed mode) don't make sense...
原因
- macOS 的
.app
本质是一个文件夹(右键显示包内容
可见),而--onefile
试图生成单个文件,两者冲突。 - 从 PyInstaller 6.0 开始已弃用此组合,未来版本会直接报错。
解决方案
✅ 改用 --onedir
模式
pyinstaller --windowed --onedir main.py
- 生成
dist/main.app
文件夹结构,符合 macOS 规范。 - 双击运行时不会出现闪退重启问题。
2. 虚拟环境 (venv
) 路径问题
错误信息
DEPRECATION: Foreign Python environment's site-packages paths added to --paths/pathex...
This is ALWAYS the wrong thing to do.
原因
- 你通过
--add-data
手动添加了虚拟环境的site-packages
,但 PyInstaller 要求必须在目标虚拟环境中运行。 - 跨环境混用会导致依赖解析失败。
解决方案
✅ 正确做法:在虚拟环境中安装并运行 PyInstaller
# 1. 激活你的虚拟环境
source /Users/01395756/Documents/pythonScripts/venv_39/bin/activate
# 2. 确认环境是否正确
which python # 应指向 venv_39 下的 Python
pip install pyinstaller # 确保在虚拟环境中安装
# 3. 重新打包(无需手动添加 site-packages)
pyinstaller --windowed --onedir main.py
3. rapidfuzz
的 Hook 警告(可忽略)
错误信息
WARNING: discover_hook_directories: Failed to process hook entry point...
AttributeError: module 'rapidfuzz.__pyinstaller' has no attribute 'get_hook_dirs'
原因
rapidfuzz
是一个优化字符串匹配的库,其 PyInstaller Hook 配置不完整。- 此警告不影响打包结果,除非你的代码直接依赖
rapidfuzz
的运行时功能。
解决方案
✅ 若无实际影响可忽略
如需彻底解决:
- 升级
rapidfuzz
:pip install --upgrade rapidfuzz
- 或手动添加 Hook 文件(复杂,通常不需要)。
4. 完整修复后的打包命令
# 1. 激活虚拟环境
source /Users/01395756/Documents/pythonScripts/venv_39/bin/activate
# 2. 清理旧构建
rm -rf build dist main.spec
# 3. 使用正确的打包方式
pyinstaller --windowed --onedir main.py
# 4. 签名应用(可选)
codesign --deep --force --sign - dist/main.app
5. 验证打包结果
- 直接运行二进制文件
./dist/main.app/Contents/MacOS/main # 应无报错
- 检查
.app
结构tree dist/main.app/Contents # 确认 Resources/MacOS 结构完整
- 双击测试
如果此前问题解决,双击main.app
应能稳定运行。
总结
问题 | 解决方案 |
---|---|
--onefile + --windowed 冲突 |
改用 --onedir |
虚拟环境路径错误 | 在目标 venv 中运行 PyInstaller |
rapidfuzz Hook 警告 |
升级库或忽略 |
按上述步骤操作后,你的应用应能正常启动,不再出现闪退重启问题。如果仍有异常,请检查是否有未捕获的运行时错误(参考控制台日志)。
最终打包命令
pyinstaller --onedir --windowed main.py
pkgbuild --root dist/main.app --identifier com.sf.pkg.main --version 1.0 --install-location "/Applications/sn_query.app" sn_query.pkg
二、打包成直接执行的工具
pyinstaller --onedir --windowed main.py
pkgbuild --root dist/main.app --identifier com.sf.pkg.main --version 1.0 --install-location "/Applications/sn_query.app" --scripts scripts sn_query_v1.1.pkg
···