'ModuleNotFoundError: 运行 PyInstaller .exe 文件时没有名为 'keras.engine.base_layer_v1'' 的模块

时间:2021-07-29 21:16:15

标签: python keras pyinstaller

我正在尝试创建 Python 脚本的可执行版本,该脚本使用 .h5 文件预测图像。该文件在虚拟环境中单独运行时完全正常。但是,当我在完成 this 之后的隐藏导入和 .spec 文件中的数据添加后运行 exe 时,当我运行 exe 时,它​​会出现以下错误:

Traceback (most recent call last):
File "onefile.py", line 11, in <module>
  model = load_model('complex_model.h5')
File "keras\saving\save.py", line 201, in load_model
File "keras\saving\hdf5_format.py", line 180, in load_model_from_hdf5
File "keras\saving\model_config.py", line 59, in model_from_config
File "keras\layers\serialization.py", line 159, in deserialize
File "keras\utils\generic_utils.py", line 668, in deserialize_keras_object
File "keras\engine\sequential.py", line 490, in from_config
File "keras\engine\training.py", line 195, in __new__
File "keras\utils\version_utils.py", line 61, in __new__
File "keras\utils\generic_utils.py", line 1181, in __getattr__
File "keras\utils\generic_utils.py", line 1172, in _load
File "importlib\__init__.py", line 127, in import_module
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'

我在以下 this post 中添加了 import keras.engine。它自己的 python 文件仍然运行良好。 但是,使用 .exe 命令创建 pyinstaller onefile.spec 文件期间没有问题:

90 INFO: PyInstaller: 4.4
90 INFO: Python: 3.8.5 (conda)
90 INFO: Platform: Windows-10-10.0.19041-SP0
90 INFO: UPX is not available.
90 INFO: Extending PYTHONPATH with paths
['path',
'path']
100 INFO: checking Analysis
360 INFO: checking PYZ
490 INFO: checking PKG
490 INFO: Bootloader path\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
490 INFO: checking EXE
500 INFO: checking COLLECT
WARNING: The output directory "path" and ALL ITS CONTENTS will be REMOVED! Continue? (y/N)y
On your own risk, you can use the option `--noconfirm` to get rid of this question.
2650 INFO: Removing dir C:\Users\ashah\models\Deeplens\Emotions\standalone\dist\onefile
3480 INFO: Building COLLECT COLLECT-00.toc
13630 INFO: Building COLLECT COLLECT-00.toc completed successfully.

.spec 的一部分发生了变化:

from PyInstaller.utils.hooks import collect_submodules
hidden_imports = collect_submodules('h5py') + collect_submodules('tensorflow_core')
block_cipher = None
a = Analysis(['onefile.py'],
         pathex=['C:\\Users\\ashah\\models\\Deeplens\\Emotions\\executable'],
         binaries=[],
         datas=[('complex_model.h5','.')],
         hiddenimports=hidden_imports,
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher,
         noarchive=False)

没有关于如何继续并让 exe 工作的想法。如果需要,我可以发布 .py 代码。任何帮助都是金。谢谢。

1 个答案:

答案 0 :(得分:0)

由于错误是由特别是 keras 引起的,我将其替换为 tensorflow.keras.* 并似乎解决了问题。