如何修复 'AttributeError' 在使用 Tensorflow 和 Keras 的项目上使用 PyInstaller?

How to fix 'AttributeError' on using PyInstaller on a project with Tensorflow and Keras?

--onefile exe PyInstaller 输出在启动时崩溃。错误是:

AttributeError: module 'tensorflow' has no attribute 'keras'
[11288] Failed to execute script main

请注意,main.py 脚本工作正常,但从未遇到过此问题。

我在 Windows 10.

中使用 PyCharm 完成所有这些工作

这些命令用于创建 .spec 文件和构建可执行文件:

pyi-makespec --onefile --name app main.py

pyinstaller --clean app.spec

检查生成的 warn-app.txt,我看到了这一行:

missing module named keras - imported by ai (top-level)

我通过在 运行 第二个命令之前将 tensorflow.keras 的路径添加到生成的 .spec 文件中来摆脱它。

路径是通过使用终端和我项目的 venv 确定的:

import tensorflow as tf
print(tf.keras.__file__)

或者:

from tensorflow import keras
print(keras.__file__)

两者都产生 <path to venv folder>\lib\site-packages\tensorflow\python\keras\api\_v2\keras\__init__.py

然后将其添加到 pathex 下的 .spec 文件中,这样:

a = Analysis(['main.py'],
             pathex=['<path to project folder>', '<path to venv folder>\lib\site-packages\tensorflow\python\keras\api\_v2\keras\__init__.py'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

再次 运行 pyinstaller --clean app.spec 时,warn-app.txt 上的缺少模块警告消失了,但生成的 --onefile 仍然崩溃错误。

需要tensorflow.keras模块的代码就这么简单:

ai.py中:

from tensorflow import keras
model = keras.load_model(`./model.hf5`)

pip freeze展示的项目基本需求(项目自己导入):

numpy==1.16.4
opencv-python==4.1.0.25
pandas==0.25.0
PyInstaller==3.5
tensorflow==2.0.0a0

扩展要求(安装基本要求时也下载安装):

absl-py==0.7.1
altgraph==0.16.1
astor==0.8.0
future==0.17.1
gast==0.2.2
google-pasta==0.1.7
grpcio==1.22.0
h5py==2.9.0
Keras==2.2.4
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.0
Markdown==3.1.1
numpy==1.17.0
opencv-python==4.1.0.25
pandas==0.25.0
pefile==2019.4.18
protobuf==3.9.0
PyInstaller==3.5
python-dateutil==2.8.0
pytz==2019.1
pywin32-ctypes==0.2.0
PyYAML==5.1.2
scipy==1.3.0
six==1.12.0
tb-nightly==1.14.0a20190301
tensorflow==2.0.0a0
termcolor==1.1.0
tf-estimator-nightly==1.14.0.dev2019030115
Werkzeug==0.15.5

整个错误:

D:\Shared\CMSC0-2\readr>readr.exe
Limited tf.compat.v2.summary API due to missing TensorBoard installation
Limited tf.summary API due to missing TensorBoard installation
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    from modules.model import models
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "d:\shared\cmsc0-2\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "modules\model.py", line 4, in <module>
    models = load_models(n=num_models)
  File "modules\ai.py", line 28, in load_models
    model = tf.keras.models.load_model(os.path.join('.', 'models', ('m' + str(i) + 'eF.h5')))
AttributeError: module 'tensorflow' has no attribute 'keras'
[11288] Failed to execute script main

老实说,我认为去掉那个丢失的模块警告会修复它,但它仍然存在,我不知道为什么。

即使 "which tensorboard" 显示 tensorboard 已经安装,"pip install tensorboard" 修复了以下错误:由于缺少 TensorBoard 而受限 tf.compat.v2.summary API安装。

降级到 tensorflow==1.14.0 为我修复了错误。

这是我的程序的基本要求:

numpy==1.16.4
opencv-python==4.1.0.25
pandas==0.25.0
PyInstaller==3.5
tensorflow==1.14.0

在我的程序的.spec文件中,我还添加了['tensorflow.keras']hiddenimports