The h5py package provides a Pythonic interface to the HDF5 binary data format, allowing users to store and manipulate large amounts of numerical data efficiently, often integrating seamlessly with NumPy arrays. It offers both high-level and low-level access to HDF5 files, datasets, and groups. The current version is 3.16.0, with development actively maintained through frequent releases.
pip install h5pyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create an HDF5 file, add groups and datasets, store NumPy arrays, attach metadata as attributes, and then read the data and attributes back. It emphasizes using context managers (`with h5py.File(...)`) for proper file handling.
Always explicitly specify the file mode (e.g., `h5py.File('file.h5', 'w')` for write, `h5py.File('file.h5', 'a')` for append, `h5py.File('file.h5', 'r+')` for read/write).Upgrade your Python environment to 3.10 or newer to use current h5py versions.
Use NumPy-style slicing to read the entire dataset: `mydataset[()]` or `mydataset[...]`.
Always use the `with h5py.File(...) as f:` context manager, which ensures the file is closed even if errors occur.
Explicitly specify the desired `dtype` when creating datasets, e.g., `group.create_dataset('name', data=my_array, dtype=np.float64)` or `group.create_dataset('name', shape=(...), dtype='f8')` for double precision.For parallel I/O, consider using multiprocessing with explicit file closing in each process, or compile h5py and HDF5 with MPI support and use `mpi4py` for true Parallel HDF5 (for writing). Parallel *read* access is generally safe from separate processes.