Skip to content

Dask thinks it loaded Xarray even if none installed since dask._compatibility.import_optional_dependency doesn't return None nor False #12086

@valeriupredoi

Description

@valeriupredoi

Describe the issue:

Hi folks, your dask._compatibility.import_optional_dependency seems not to work correctly, since even if there is absolutely no Xarray installed in the current environment, that utility still returns some type of strings, definitely not None nor False as it should; consequently, Dask thinks Xarray is there and is trying to access modules like xr.DataArray, which invariably fails.

Minimal Complete Verifiable Example:

from dask._compatibility import import_optional_dependency

xr = import_optional_dependency("xarray", errors="ignore")
print(xr)

that returns a dummy module <module 'xarray' (namespace) from ['/home/valeriu/xarray']> in my case, which is enough for Dask to be happy xarray was successfully imported. And yes, I do have an xarray dir in my $HOME, which is Xarray's source code cloned from GitHub, but it's not installed in any way.

Anything else we need to know?:

This affects higher level tools like Scitools iris SciTools/iris#6723 or anyone else that doesn't have Xarray as direct dependency from their package.

Environment:

  • Dask version: 2025.09.1 - conda-forge
  • Python version: 3.13.7
  • Operating System: Ubuntu 24.04.3 LTS
  • Install method (conda, pip, source): conda-forge

Cheers 🍻

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething is brokenneeds infoNeeds further information from the user

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions