diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 77d6296acac..2d4cf6771ab 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -61,6 +61,7 @@ New Features :py:class:`~core.groupby.DataArrayGroupBy`, inspired by pandas' :py:meth:`~pandas.core.groupby.GroupBy.get_group`. By `Deepak Cherian `_. +- Disable the `cfgrib` backend if the `eccodes` library is not installed (:pull:`5083`). By `Baudouin Raoult `_. Breaking changes ~~~~~~~~~~~~~~~~ diff --git a/xarray/backends/cfgrib_.py b/xarray/backends/cfgrib_.py index c63e1543746..92c3fb0b286 100644 --- a/xarray/backends/cfgrib_.py +++ b/xarray/backends/cfgrib_.py @@ -1,4 +1,5 @@ import os +import warnings import numpy as np @@ -20,7 +21,12 @@ has_cfgrib = True except ModuleNotFoundError: has_cfgrib = False - +# cfgrib throws a RuntimeError if eccodes is not installed +except RuntimeError: + warnings.warn( + "Failed to load cfgrib - most likely eccodes is missing. Try `import cfgrib` to get the error message" + ) + has_cfgrib = False # FIXME: Add a dedicated lock, even if ecCodes is supposed to be thread-safe # in most circumstances. See: