From 12b64781b0ec4a82a8e06b512ce9d512c4892363 Mon Sep 17 00:00:00 2001 From: "Lumberbot (aka Jack)" <39504233+meeseeksmachine@users.noreply.github.com> Date: Mon, 11 Nov 2024 08:18:12 -0800 Subject: [PATCH] Backport PR #1754 on branch 0.11.x ((fix): resolve cupy import error on `cupy-cuda12x<0.13`) (#1755) Co-authored-by: Ilan Gold --- docs/release-notes/1754.bugfix.md | 1 + src/anndata/compat/__init__.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 docs/release-notes/1754.bugfix.md 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