Skip to content

Conversation

@NatalieCao323
Copy link

This PR fixes the ImportError: cannot import name 'read' from 'anndata' that occurs when using dynamo-release with anndata >= 0.10.

Problem

The anndata.read() function was deprecated in anndata 0.8 and removed in anndata 0.10.0 (released December 2023). Dynamo-release v1.4.1 still imports this function directly, causing an ImportError for all users with modern anndata versions.

Error:

ImportError: cannot import name 'read' from 'anndata'

Location: dynamo/data_io.py, line 9

Impact

This issue affects:

  • All users with anndata >= 0.10 (released Dec 2023)
  • All new installations (pip installs latest anndata by default)
  • All downstream packages (spateo-release, etc.)

Solution

Added a compatibility wrapper that:

  1. Tries to import read from anndata (for old versions)
  2. Falls back to read_h5ad if read doesn't exist (for new versions)
  3. Maintains backward compatibility with anndata < 0.10
  4. Ensures forward compatibility with anndata >= 0.10

Changes

File: dynamo/data_io.py

Before:

from anndata import (
    AnnData,
    read,  # ❌ This doesn't exist in anndata >= 0.10
    read_csv,
    # ... other imports
)

After:

from anndata import (
    AnnData,
    read_csv,
    # ... other imports (removed 'read' from here)
)

# Compatibility with anndata >= 0.10
# The 'read' function was deprecated and removed in anndata 0.10.0
# Use read_h5ad as a fallback for backward compatibility
try:
    from anndata import read
except ImportError:
    # anndata >= 0.10 removed 'read', use 'read_h5ad' instead
    from anndata import read_h5ad as read

Testing

Tested with:

  • anndata 0.9.2 (old version) - Works
  • anndata 0.10.9 (new version) - Works
  • anndata 0.12.2 (latest) - Works
  • Python 3.11
  • macOS, Linux (expected to work on Windows too)

Verification:

# Import test
import dynamo as dyn  # ✅ No ImportError
print(dyn.__version__)  # ✅ 1.4.1

# Downstream test
import spateo as st  # ✅ Now works!
print(st.__version__)  # ✅ 1.1.1

The 'read' function was deprecated in anndata 0.8 and removed in 0.10.0.
This commit adds a compatibility wrapper that uses read_h5ad as a fallback
when the read function is not available, ensuring compatibility with both
old and new versions of anndata.

Fixes ImportError: cannot import name 'read' from 'anndata'

This issue was blocking spateo-release and other downstream packages
from working with anndata >= 0.10.
@codecov
Copy link

codecov bot commented Oct 13, 2025

Codecov Report

❌ Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 36.56%. Comparing base (9f82563) to head (746d85d).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
dynamo/data_io.py 50.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #730   +/-   ##
=======================================
  Coverage   36.56%   36.56%           
=======================================
  Files         183      183           
  Lines       35352    35334   -18     
=======================================
- Hits        12925    12919    -6     
+ Misses      22427    22415   -12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Xiaojieqiu
Copy link
Collaborator

looks good. I am merging it now

@Xiaojieqiu Xiaojieqiu merged commit c1ed873 into aristoteleo:master Oct 13, 2025
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants