使用PyInstaller打包含CEFPython的Tkinter程序时遇ICU数据错误
解决PyInstaller打包CEFPython+Tkinter应用时的ICU数据错误
错误原因
ERROR:icu_util.cc(133)] Invalid file descriptor to ICU data received 是因为CEF依赖的ICU国际化数据文件(icudtl.dat)未被正确打包到可执行文件中,运行时无法定位到该文件导致的。
解决方案
方案1:使用PyInstaller自动收集依赖参数
修改打包命令,添加--collect-all cefpython3参数,让PyInstaller自动收集CEFPython的所有依赖资源:pyinstaller -i assets/icon.ico -F -w --collect-all cefpython3 bot.py方案2:代码中手动指定CEF资源路径
在初始化CEF前,判断是否为打包后的环境,手动指定资源文件路径:import os import sys from cefpython3 import cefpython as cef if getattr(sys, 'frozen', False): # 获取打包后的临时资源目录 base_path = sys._MEIPASS cef.Initialize(settings={ "resources_dir_path": os.path.join(base_path, "cefpython3"), "locales_dir_path": os.path.join(base_path, "cefpython3", "locales"), }) else: cef.Initialize()方案3:编辑PyInstaller spec文件手动添加资源
- 生成spec文件:
pyinstaller -i assets/icon.ico -F -w --collect-all cefpython3 bot.py --specpath . - 打开生成的
bot.spec,在datas列表中添加ICU文件和 locales 目录:import os from cefpython3 import cefpython as cef datas += [ (os.path.join(os.path.dirname(cef.__file__), "icudtl.dat"), "cefpython3"), (os.path.join(os.path.dirname(cef.__file__), "locales"), "cefpython3/locales"), ] - 用spec文件重新打包:
pyinstaller bot.spec
- 生成spec文件:
方案4:放弃单文件模式(-F参数)
单文件模式下,CEF的依赖文件会被解压到临时目录,可能出现路径识别问题。改用目录模式打包:pyinstaller -i assets/icon.ico -w --collect-all cefpython3 bot.py打包后运行
dist/bot目录下的可执行文件即可。
内容的提问来源于stack exchange,提问作者Cassano
相关产品推荐
相关产品推荐

