5

My python scrip includes:

from keras.models import model_from_json
model = model_from_json(open("test.json").read())
model.load_weights("test.h5")
model.compile(loss="mean_squared_error", optimizer = "adam")

Then, I created an exe file using pyinstaller from aforementioned script. The exe file can not load the saved model. Any thought on that would be appreciated.

andre
  • 551
  • 1
  • 5
  • 8
  • The error message would be helpful to diagnose a problem. Are you creating exe in one-file mode or one-dir mode? Is `test.h5` placed near executable? – 9dogs Aug 23 '17 at 07:17
  • Initially i used this: pyinstaller -w myscript.py which create the exe and dependent libraries in a directory. and error : ModuleNotFoundError: No module named 'h5py.defs' ModuleNotFoundError: No module named 'h5py.utils' I already imported h5py: import h5py I used this command to resolve the error: pyinstaller -w --hidden-import=h5py.defs --hidden-import=h5py.utils myscript.py and i got this error: ModuleNotFoundError: No module named 'h5py.h5ac' – andre Aug 23 '17 at 14:17
  • my suggestion was too long - moved it to answer. Sorry if it won't help. – 9dogs Aug 23 '17 at 14:42
  • It did helped. Thank you for your time. – andre Aug 23 '17 at 16:17

2 Answers2

6

If you get errors about h5py submodules, try to use collect_submodules function to add them all to hidden_imports.

You probably noticed a file called myscript.spec generated by a pyinstaller. Inside this file is an instruction on how to build you script (and it's just a python code too!).

So try to edit this myscript.spec like this:

from PyInstaller.utils.hooks import collect_submodules

hidden_imports = collect_submodules('h5py')

a = Analysis(['myscript.py'],
         binaries=None,
         datas=[],
         hiddenimports=hidden_imports,
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=None)

# ... rest of a file untouched

Then run pyinstaller against that file: pyinstaller myscript.spec.

9dogs
  • 1,446
  • 10
  • 18
2

This resolved the error:

pyinstaller -w --hidden-import=h5py.defs --hidden-import=h5py.utils --hidden-import=h5py.h5ac --hidden-import=h5py._proxy myscript.py

andre
  • 551
  • 1
  • 5
  • 8