本文介绍了'ModuleNotFoundError:运行PyInstaller.exe文件时没有名为'keras.Engine.base_layer_v1''的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Traceback (most recent call last):
File "onefile.py", line 11, in <module>
  model = load_model('complex_model.h5')
File "kerassavingsave.py", line 201, in load_model
File "kerassavinghdf5_format.py", line 180, in load_model_from_hdf5
File "kerassavingmodel_config.py", line 59, in model_from_config
File "keraslayersserialization.py", line 159, in deserialize
File "kerasutilsgeneric_utils.py", line 668, in deserialize_keras_object
File "kerasenginesequential.py", line 490, in from_config
File "kerasengine	raining.py", line 195, in __new__
File "kerasutilsversion_utils.py", line 61, in __new__
File "kerasutilsgeneric_utils.py", line 1181, in __getattr__
File "kerasutilsgeneric_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 pathlibsite-packagesPyInstallerootloaderWindows-64bit
un.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:UsersashahmodelsDeeplensEmotionsstandalonedistonefile
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)
关于如何继续并使可执行文件工作的想法不足。如果需要,我可以发布.py代码。任何帮助都是金子。谢谢。

推荐答案

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

这篇关于&#39;ModuleNotFoundError:运行PyInstaller.exe文件时没有名为&#39;keras.Engine.base_layer_v1&#39;&#39;的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 03:38
查看更多