diff --git a/docs/release-notes/1754.bugfix.md b/docs/release-notes/1754.bugfix.md new file mode 100644 index 000000000..492553ef1 --- /dev/null +++ b/docs/release-notes/1754.bugfix.md @@ -0,0 +1 @@ +Fix `cupy<0.13` imports in non-gpu environments {user}`ilan-gold` diff --git a/src/anndata/compat/__init__.py b/src/anndata/compat/__init__.py index b5ec3d415..255ffa548 100644 --- a/src/anndata/compat/__init__.py +++ b/src/anndata/compat/__init__.py @@ -140,7 +140,16 @@ def __repr__(): return "mock dask.array.core.Array" -if find_spec("cupy") or TYPE_CHECKING: +# https://github.com/scverse/anndata/issues/1749 +def is_cupy_importable() -> bool: + try: + import cupy # noqa: F401 + except ImportError: + return False + return True + + +if is_cupy_importable() or TYPE_CHECKING: from cupy import ndarray as CupyArray from cupyx.scipy.sparse import csc_matrix as CupyCSCMatrix from cupyx.scipy.sparse import csr_matrix as CupyCSRMatrix