'ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'' when running PyInstaller .exe file

'ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'' when running PyInstaller .exe file

我正在尝试创建 python 脚本的可执行版本,该脚本使用 .h5 文件预测图像。文件 运行 在虚拟环境中单独使用时完全没问题。但是,当我 运行 exe 在完成 this 之后的隐藏导入和 .spec 文件中的数据添加后,当我 运行 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'

我在下面的里加了import keras.engine。 python 文件本身仍然 运行 很好。 但是,在使用 pyinstaller onefile.spec 命令创建 .exe 文件期间没有问题:

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 工作的想法。如果需要,我可以 post .py 代码。任何帮助都是黄金。谢谢。

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

我遇到了同样的问题,但错误代码是

ModuleNotFoundError: No module named 'tensorflow.python.keras.engine.base_layer_v1'

通过在 .spec 文件中添加隐藏导入来解决它:

hiddenimports=['tensorflow.python.keras.engine.base_layer_v1'],

或在编译时 .exe :

--hidden-import=tensorflow.python.keras.engine.base_layer_v1