Keras: ImportError: No module named h5py

Created on 9 Aug 2016  路  34Comments  路  Source: keras-team/keras

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 "", line 1, in
File "keras/init.py", line 4, in
from . import engine
File "keras/engine/init.py", line 3, in
from .topology import InputSpec
File "keras/engine/topology.py", line 17, in
from ..utils.io_utils import ask_to_proceed_with_overwrite
File "keras/utils/io_utils.py", line 3, in
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?

Most helpful comment

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

Try running pip install h5py

All 34 comments

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

Try running pip install h5py

If that fails then you may also need libhdf5 :)

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

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

Same issue with keras 1.0.7 (didn't happen with 1.0.5) on Mac OSX El Capitan, just had to manually add h5py via pip. I didn't get prompted for either Cython or having a missing libhdf5

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

pip install h5py
pip install cython

solved my problem in Windows 10

Same problem on Debian and python2

mportError: save_model requires h5py.

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.

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.

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

pip install h5py worked well for me

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

@vmalyi I dont know how, but it fixed for me :)

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 had this issue when both pip and conda had somehow ended up with different h5py versions. Removing it from pip entirely (only keeping it for conda), solved this issue. For the record, I kept libhdf5 and python-h5py.

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鈥檛 need to import it before calling it.

@colllin I try to do the same, but I get "ImportError: cannot import name reload". I am using Python 2.7 and I have installed h5py, now trying to save models without restarting kernel.

@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.

sudo apt-get install python-h5py

for keras 2.1.2 running on mac use:

pip install h5py

just for me after restarting the computer

it worked for me when i Install the h5py package by using conda, and restart the Jupyter notebook.

just to summarize, you need to install:

pip install h5py

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

In win10, just excute the command: pip install h5py.

I am able to import h5Py in Anaconda prompt and ipython but it doesnt work in Spyder. Not able to figure out what might be going wrong. I have python 3.6.4 on windows 7 64 bit machine.

Have you checked to see if h5py is in your site-packages dir? I ran into this problem with tensorflow using Jupyter, until I realised conda had installed into a different env (called tfdeeplearning).

You can check where your /site-packages dir is with following code:
import sys
sys.path

By default conda will install into its lib dir, e.g: ~/anacoda3/lib (which is generally used by ipython on cmd line).
In the case of tensorflow the path was ~/anaconda3/envs/tfdeeplearning. I wonder if there is something similar for spyder.

If this is the case you can install into a specific env location as follows:
conda install -n h5py

In my case the following fixed the problem:
conda install -n tfdeeplearning h5py

I am running OSX 10.13, but I expect it could be similar in Windows.

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

Nothing helps.
I have

h5py==2.8.0
html5lib==0.9999999
keras==2.1.6

I installed libhdf5-dev and python-h5py still can't load.

code$ dpkg -s libhdf5-dev
Package: libhdf5-dev
Status: install ok installed
Priority: optional
Section: libdevel
Installed-Size: 29771
Maintainer: Ubuntu Developers <[email protected]>
Architecture: amd64
Source: hdf5
Version: 1.8.16+docs-4ubuntu1
Replaces: libhdf5-serial-dev (<< 1.8.12-9~)
Provides: libhdf5-serial-dev
Depends: libhdf5-10 (= 1.8.16+docs-4ubuntu1), zlib1g-dev, libjpeg-dev, libaec-dev, hdf5-helpers, libhdf5-cpp-11 (= 1.8.16+docs-4ubuntu1)
Suggests: libhdf5-doc
Breaks: libhdf5-serial-dev (<< 1.8.12-9~)
Description: Hierarchical Data Format 5 (HDF5) - development files - serial version
 HDF5 is a file format and library for storing scientific data.
 HDF5 was designed and implemented to address the deficiencies of
 HDF4.x. It has a more powerful and flexible data model, supports
 files larger than 2 GB, and supports parallel I/O.
 .
 This package contains development files for serial platforms.
Homepage: http://hdfgroup.org/HDF5/
Original-Maintainer: Debian GIS Project <[email protected]>

@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

Closing as this is resolved

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

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

I tried all the solutions above, it yet doesn't work well. I hope someone would like to help me. My environement is Anocada(python3.6.3). The following is the detail error in the xshell(ubuntu serve)

Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 12, in
import subprocess, tempfile, os.path, re, pwd, grp, os, time
File "/usr/lib/python3.6/subprocess.py", line 136, in
import _posixsubprocess
ModuleNotFoundError: No module named '_posixsubprocess'

Original exception was:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.6/dist-packages/h5py/__init__.py", line 26, in
from . import _errors
File "h5py/_errors.pyx", line 13, in init h5py._errors
File "/usr/local/lib/python3.6/dist-packages/h5py/_hl/compat.py", line 5, in
import six
File "/usr/lib/python3/dist-packages/six.py", line 631, in
import struct
File "/usr/lib/python3.6/struct.py", line 13, in
from _struct import *
ModuleNotFoundError: No module named '_struct'

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 have win 10 as well, uninstalling h5py and re-installing worked totally for me :)

Was this page helpful?
0 / 5 - 0 ratings