keras-team/keras · error · ImportError

`load_model()` using h5 format requires h5py. Could not…

Error message

`load_model()` using h5 format requires h5py. Could not import h5py.

What it means

Raised when `load_model()` is called on an H5 model file but the `h5py` package is not installed. H5-format models require h5py. Install it with `pip install h5py`, or save/load the model in the native Keras format (.keras) instead.

Solutions

  1. pip install h5py in the loading environment
  2. Add h5py to deployment requirements if you ship .h5 checkpoints
  3. Convert .h5 files to .keras once, in an environment that has h5py, to drop the dependency

Example fix

# before
model = keras.saving.load_model('model.h5')  # ImportError
# after
# pip install h5py
model = keras.saving.load_model('model.h5')
Defensive patterns

Strategy: fallback

Validate before calling

try:
    import h5py
except ImportError:
    raise SystemExit('pip install h5py or convert the file to .keras')

Try / catch

try:
    keras.saving.load_model('m.h5')
except ImportError:
    raise SystemExit('h5py missing: pip install h5py')

Prevention

When it happens

Trigger: keras.saving.load_model('model.h5') or load_model_from_hdf5 on a machine without h5py, e.g. a minimal inference container.

Common situations: Training image had h5py but the deployment image does not; CI runners installing only runtime deps.

Understand the failure class

Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/1bac2f3444d7129b. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/legacy/saving/legacy_h5_format.py:106

            (strings) to custom classes or functions to be
            considered during deserialization.
        compile: Boolean, whether to compile the model
            after loading.

    Returns:
        A Keras model instance. If an optimizer was found
        as part of the saved model, the model is already
        compiled. Otherwise, the model is uncompiled and
        a warning will be displayed. When `compile` is set
        to `False`, the compilation is omitted without any
        warning.

    Raises:
        ImportError: if h5py is not available.
        ValueError: In case of an invalid savefile.
    """
    if h5py is None:
        raise ImportError(
            "`load_model()` using h5 format requires h5py. Could not "
            "import h5py."
        )

    if not custom_objects:
        custom_objects = {}

    gco = object_registration.GLOBAL_CUSTOM_OBJECTS
    tlco = global_state.get_global_attribute("custom_objects_scope_dict", {})
    custom_objects = {**custom_objects, **gco, **tlco}

    opened_new_file = not isinstance(filepath, h5py.File)
    if opened_new_file:
        f = h5py.File(filepath, mode="r")
    else:
        f = filepath

    model = None

View on GitHub (pinned to 7a34a03db6)