![](/img/trans.png)
[英]Problems bundling Tensorflow with Pyinstaller - No module named 'tensorflow.python.keras.engine.base_layer_v1'
[英]'ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'' when running PyInstaller .exe file
我正在嘗試創建 python 腳本的可執行版本,該腳本使用.h5
文件預測圖像。 該文件在虛擬環境中單獨運行時完全正常。 但是,當我在完成隱藏導入之后運行 exe 並在.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 工作的想法用完了。 如果需要,我可以發布 .py 代碼。 任何幫助都是黃金。 謝謝。
我有同樣的問題,但錯誤代碼是
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
由於錯誤是由keras
引起的,我將其替換為tensorflow.keras.*
似乎解決了問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.