-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
What happened?
Up until the last release, it was possible to do dt.to_netcdf("filename.nc")
without specifying the engine independently of which of netcdf4 of h5netcdf were installed. After #10571 engine=None
is interpreted as h5netcdf independently of available libraries. This diverges from the other to_netcdf
methods and goes against the docstring of DataTree.to_nectdf:
engine ({"netcdf4", "h5netcdf"}, optional) – Engine to use when writing netCDF files. If not provided, the default engine is chosen based on available dependencies, with a preference for “netcdf4” if writing to a file on disk.
What did you expect to happen?
dt.to_netcdf("filename")
works even if only netcdf4 is installed and h5netcdf is not.
Minimal Complete Verifiable Example
ds1 = xr.Dataset({"foo": "orange"})
dt = xr.DataTree(name="root", dataset=ds1)
dt.to_netcdf("file.nc")
# full traceback below
dt.to_netcdf("file.nc", engine="netcdf4") # works
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
dt.to_netcdf("file.nc")
~~~~~~~~~~~~^^^^^^^^^^^
File "/home/oriol/bin/miniforge3/envs/xarray-minimal/lib/python3.13/site-packages/xarray/core/datatree.py", line 1834, in to_netcdf
return _datatree_to_netcdf(
self,
...<9 lines>...
**kwargs,
)
File "/home/oriol/bin/miniforge3/envs/xarray-minimal/lib/python3.13/site-packages/xarray/core/datatree_io.py", line 77, in _datatree_to_netcdf
ds.to_netcdf(
~~~~~~~~~~~~^
target,
^^^^^^^
...<7 lines>...
**kwargs,
^^^^^^^^^
)
^
File "/home/oriol/bin/miniforge3/envs/xarray-minimal/lib/python3.13/site-packages/xarray/core/dataset.py", line 2109, in to_netcdf
return to_netcdf( # type: ignore[return-value] # mypy cannot resolve the overloads:(
self,
...<10 lines>...
auto_complex=auto_complex,
)
File "/home/oriol/bin/miniforge3/envs/xarray-minimal/lib/python3.13/site-packages/xarray/backends/api.py", line 2078, in to_netcdf
store = store_open(target, mode, format, group, **kwargs)
File "/home/oriol/bin/miniforge3/envs/xarray-minimal/lib/python3.13/site-packages/xarray/backends/h5netcdf_.py", line 169, in open
import h5netcdf
ModuleNotFoundError: No module named 'h5netcdf'
Anything else we need to know?
I would propose as fix instead of if engine is None: engine="h5netcdf"
to call xarray.backends.api._get_default_engine_netcdf()
instead, but I also skimmed #10571 and I am not sure if we should remove scipy as an output of the function when neither netcdf4 nor h5netcdf are installed.
Environment
INSTALLED VERSIONS
commit: None
python: 3.13.5 | packaged by conda-forge | (main, Jun 16 2025, 08:27:50) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 6.14.0-27-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: ('en_GB', 'UTF-8')
libhdf5: 1.14.6
libnetcdf: 4.9.2
xarray: 2025.8.0
pandas: 2.3.1
numpy: 2.3.2
scipy: None
netCDF4: 1.7.2
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: 1.6.4
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: 25.2
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None