1

I am trying to create an executable version of python script that predicts images using .h5 file. The file runs completely fine when on its own in the virtual environment. But when I run the exe after completing the hidden imports following this and data addition in .spec file, when I run the exe it gives the following error:

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'

I added import keras.engine in the following this post. The python file on it's own still runs well. However, there are no issues during the creation of .exe file with pyinstaller onefile.spec command:

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.

Here is a part of .spec that was changed:

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)

Running out of ideas on how to proceed and get the exe working. I can post the .py code if needed. Any help would be golden. Thanks.

abhishah901
  • 539
  • 1
  • 8
  • 16

2 Answers2

1

I had the same issue, but the error code was

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

Resolved it by adding hidden import, either in .spec file:

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

or while compiling .exe :

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

Since the error is caused by keras in particular, I replaced it with tensorflow.keras.* and seemed to resolve the issue.

abhishah901
  • 539
  • 1
  • 8
  • 16
  • Did you replace it in your .py source file? Or did you add it as a hidden import? – Aelius Dec 01 '21 at 21:46
  • @Aelius I changed it in the ```.spec``` file – abhishah901 Dec 03 '21 at 21:15
  • @abhishah901 What did you change in the `.spec` file? I don't have anything that could correspond to `keras` or `tensorflow.keras.*` –  Feb 07 '22 at 15:54
  • @AlexZab I previously had ```collect_submodules('keras')``` instead of ```collect_submodules('tensorflow_core')```. Tensorflow comes with keras within itself so it takes care of that. Does that help? – abhishah901 Feb 10 '22 at 02:21