title: 'ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'' when running PyInstaller .exe file
body: <p>I am trying to create an executable version of python script that predicts images using <code>.h5</code> 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 <a href="https://stackoverflow.com/questions/45830206/pyinstaller-created-exe-file-can-not-load-a-keras-nn-model">this</a> and data addition in <code>.spec</code> file, when I run the exe it gives the following error:</p>
  <pre><code>Traceback (most recent call last):
  File &quot;onefile.py&quot;, line 11, in &lt;module&gt;
    model = load_model('complex_model.h5')
  File &quot;keras\saving\save.py&quot;, line 201, in load_model
  File &quot;keras\saving\hdf5_format.py&quot;, line 180, in load_model_from_hdf5
  File &quot;keras\saving\model_config.py&quot;, line 59, in model_from_config
  File &quot;keras\layers\serialization.py&quot;, line 159, in deserialize
  File &quot;keras\utils\generic_utils.py&quot;, line 668, in deserialize_keras_object
  File &quot;keras\engine\sequential.py&quot;, line 490, in from_config
  File &quot;keras\engine\training.py&quot;, line 195, in __new__
  File &quot;keras\utils\version_utils.py&quot;, line 61, in __new__
  File &quot;keras\utils\generic_utils.py&quot;, line 1181, in __getattr__
  File &quot;keras\utils\generic_utils.py&quot;, line 1172, in _load
  File &quot;importlib\__init__.py&quot;, line 127, in import_module
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1014, in _gcd_import
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 991, in _find_and_load
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 973, in _find_and_load_unlocked
  ModuleNotFoundError: No module named 'keras.engine.base_layer_v1'
  </code></pre>
  <p>I added <code>import keras.engine</code> in the following <a href="https://stackoverflow.com/questions/51337558/how-to-import-keras-engine-topology-in-tensorflow">this post</a>. The python file on it's own still runs well.
  However, there are no issues during the creation of <code>.exe</code> file with <code>pyinstaller onefile.spec</code> command:</p>
  <pre><code>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 &quot;path&quot; 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.
  </code></pre>
  <p>Here is a part of <code>.spec</code> that was changed:</p>
  <pre><code>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)
  </code></pre>
  <p>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.</p>