You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyInstaller将.py转.exe时遇AttributeError问题求助

问题原因及解决方案

核心原因

PyInstaller打包时未正确识别google.generativeai库的动态导入结构或依赖模块,导致生成的.exe文件中缺失GenerativeModel相关代码;另外也可能是打包环境与Jupyter运行环境的库版本不一致。

具体解决步骤

  • 确认环境依赖一致性:分别在Jupyter运行环境和打包环境中执行pip show google-generativeai,确保两个环境的库版本完全相同。若版本不一致,在打包环境中安装对应版本:pip install google-generativeai==x.x.x(替换x.x.x为Jupyter环境的版本号)。

  • 手动指定隐藏导入:使用PyInstaller的--hidden-import参数强制包含缺失的模块,打包命令示例:

pyinstaller --hidden-import=google.generativeai --hidden-import=google.generativeai.generative_models your_script.py
  • 全量收集库文件:若隐藏导入仍无效,使用--collect-all参数让PyInstaller收集google-generativeai的所有文件,避免遗漏依赖:
pyinstaller --collect-all google-generativeai your_script.py
  • 修改.spec文件(可选):如果需要多次打包,可编辑生成的.spec文件,在hiddenimports列表中添加相关模块:
hiddenimports=['google.generativeai', 'google.generativeai.generative_models']

然后执行pyinstaller your_script.spec重新打包。

内容的提问来源于stack exchange,提问作者조연서

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 16:12:04