keras: ImportError: No module named h5py

on Ubuntu 14.04.5 LTS

installed keras from scratch as below :

  1. installed Cuda Toolkit 7.5
  2. installed cuDNN v4
  3. installed python-pip, python-dev using apt-get
  4. installed tensorflow 0.9.0 (later upgraded to 0.9.0rc)
  5. installed keras by downloading from here https://github.com/fchollet/keras.git and extract, and typing command line : sudo pip install keras

however, it didn’t work properly, I remembered it was a scipy problem so I installed scipy and again tried to install keras by the same command : sudo pip install keras but I only got these messages :

Requirement already up-to-date: keras in /usr/local/lib/python2.7/dist-packages Requirement already up-to-date: theano in /usr/local/lib/python2.7/dist-packages (from keras) Requirement already up-to-date: pyyaml in /usr/local/lib/python2.7/dist-packages (from keras) Requirement already up-to-date: six in /usr/local/lib/python2.7/dist-packages (from keras) Requirement already up-to-date: numpy>=1.7.1 in /usr/local/lib/python2.7/dist-packages (from theano->keras) Requirement already up-to-date: scipy>=0.11 in /usr/local/lib/python2.7/dist-packages (from theano->keras) Cleaning up…

and on the terminal by typing “python” I tried to import Sequential from keras.models only to check if keras was installed properly but got theses errors :

File “<stdin>”, line 1, in <module> File “keras/init.py”, line 4, in <module> from . import engine File “keras/engine/init.py”, line 3, in <module> from .topology import InputSpec File “keras/engine/topology.py”, line 17, in <module> from …utils.io_utils import ask_to_proceed_with_overwrite File “keras/utils/io_utils.py”, line 3, in <module> import h5py

Originally, the error occured when importing module backend so I upgraded my tensorflow and after that the above errors occured. (I edited the home./keras/keras.json to change the backend from theano to tensorflow)

I installed anaconda just because it is equipped with many useful python packages but it didn’t work for this problem.

What should I do?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 34 (2 by maintainers)

Most upvoted comments

Have you tried directly installing h5py? http://docs.h5py.org/en/latest/build.html

Try running pip install h5py

I got the problem while I adopt acenewgate’s opinion, but it’s not matter. The following solve my problem. (My environment is ubuntu 14.04)

  1. sudo pip install cython
  2. sudo apt-get install libhdf5-dev
  3. sudo pip install h5py

solved problems by first installing libhdf5 and then installing h5py

  1. sudo apt-get install libhdf5
  2. sudo pip install h5py thanks to both of you

you can only type: sudo apt-get install python-h5py

@krishnadusad after pip3 install h5py restarting the jupyter notebook solves the problem.

In Jupyter, I didn’t want to restart my kernel after !pip install h5py (because I wanted to save that model!). This allowed me to continue without restarting:

from importlib import reload
reload(keras.models)

Update for Python 2.7 users: reload is a built-in so you don’t need to import it before calling it.

pip install h5py pip install cython

solved my problem in Windows 10

I can run h5py tests but Keras still gives me errors. Any ideas on what could help?

Update 1: Restarting jupyter notebook/ restarting my computer fixed it.

On windows 10, I executed

pip install h5py
pip install cython

But I still get the same error.

I tried to import h5py directly and test it, then I get this error:

>>> import h5py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Gautam\Anaconda2\envs\dlwin36\lib\site-packages\h5py\__init__.py", line 34, in <module>
    from ._conv import register_converters as _register_converters
  File "h5py\h5r.pxd", line 21, in init h5py._conv (C:\Minonda\conda-bld\h5py_1496869654914\work\h5py\_conv.c:7550)
  File "h5py\_objects.pxd", line 12, in init h5py.h5r (C:\Minonda\conda-bld\h5py_1496869654914\work\h5py\h5r.c:3236)
  File "h5py\_objects.pyx", line 1, in init h5py._objects (C:\Minonda\conda-bld\h5py_1496869654914\work\h5py\_objects.c:7762)
ImportError: DLL load failed: The specified procedure could not be found.

UPDATE: ok…so I uninstalled h5py and then reinstalled it and restarted anaconda. Now its working!!!

I recently had the same problem… I was executing a python script from a bash file… I used python3 , on which I didn’t have h5py installed.

pip3 install h5py

solved my problem.

This issue is happening because of different version of keras and h5py.

Steps to solve

  1. Remove “h5py” if you already installed pip uninstall h5py

  2. Remove keras pip uninstall keras

3 . Now, Install keras, This will install updated and same version of keras and h5py pip install keras

Same problem on Debian and python2

mportError: save_model requires h5py.

If that fails then you may also need libhdf5 😃

@wt-huang please close this issue, we’re repeating the same thing over and over.

Just for your information. I am using Raspberry pi (Raspbian Stretch) when I have the similar issue showing that keras can not load module h5py

By doing the following solved my problem: sudo apt-get install libhdf5-dev

@iamtodor maybe you should try it in your workspace. In my case, I tried pip install h5py in anaconda prompt. But it still error in Tensorflow Spyder. So i add h5py in tensorflow by following, then it works. activate tensorflow pip install h5py

just to summarize, you need to install:

pip install h5py

If it does not help, please restart your jupyther notebook or computer.

@colllin It is solved. Actually, I did not need to import reload (it was imported). so, I just ran reload(keras.models) and then I could save my models.

Can’t reproduce this issue with Keras (2.0.5) and Python 3.6.1

pip install h5py worked well for me