【问题标题】:'ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'' when running PyInstaller .exe file'ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'' 运行 PyInstaller .exe 文件时
【发布时间】:2021-07-29 21:16:15
【问题描述】:

我正在尝试创建一个 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文件仍然运行良好。 但是,使用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 工作的想法已经用尽。如果需要,我可以发布 .py 代码。任何帮助都是金。谢谢。

【问题讨论】:

    标签: python keras pyinstaller


    【解决方案1】:

    我有同样的问题,但错误代码是

    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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 您是否在 .py 源文件中替换了它?还是您将其添加为隐藏导入?
      • @Aelius 我在.spec 文件中更改了它
      • @abhishah901 您在.spec 文件中做了哪些更改?我没有任何可以对应于keras 或tensorflow.keras.*
      • @AlexZab 我以前有collect_submodules('keras') 而不是collect_submodules('tensorflow_core')。 Tensorflow 本身带有 keras,所以它会处理这个问题。这有帮助吗?
      猜你喜欢
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-27
      • 2022-12-27
      • 2022-12-19
      相关资源
      最近更新 更多