scverse / squidpy

Spatial Single Cell Analysis in Python
https://squidpy.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
433 stars 79 forks source link

ImportError: cannot import name 'get_cmap' from 'matplotlib.cm' #853

Closed zyll123 closed 3 months ago

zyll123 commented 3 months ago

Description

I installed suidpy in a virtual environment. When I import the squidpy, an import error occurred : ImportError: cannot import name 'get_cmap' from 'matplotlib.cm'

Minimal reproducible example

 micromamba install -c conda-forge squidpy
import squidpy as sq

I searched the internet and found it might be the problem caused by newest version of matplotlib, so I reinstalled matplotlib 3.8.3, but the same problem exited.

micromamba remove matplotlib
micromamba install matplotlib=3.8.3

Traceback

```pytb ImportError Traceback (most recent call last) Cell In[1], line 1 ----> 1 import squidpy as sq File /public/home/yzhang/micromamba/envs/Spatial/lib/python3.9/site-packages/squidpy/__init__.py:1 ----> 1 from squidpy import gr, im, pl, read, datasets 3 __author__ = __maintainer__ = "Theislab" 4 __email__ = ", ".join( 5 [ 6 "giovanni.palla@helmholtz-muenchen.de", 7 "hannah.spitzer@helmholtz-muenchen.de", 8 ] 9 ) File /public/home/yzhang/micromamba/envs/Spatial/lib/python3.9/site-packages/squidpy/pl/__init__.py:11 9 from squidpy.pl._utils import extract 10 from squidpy.pl._ligrec import ligrec ---> 11 from squidpy.pl._spatial import spatial_scatter, spatial_segment 12 from squidpy.pl._interactive import Interactive # type: ignore[attr-defined] File /public/home/yzhang/micromamba/envs/Spatial/lib/python3.9/site-packages/squidpy/pl/_spatial.py:15 13 from squidpy.gr._utils import _assert_spatial_basis 14 from squidpy.pl._utils import save_fig, sanitize_anndata ---> 15 from squidpy.pl._spatial_utils import ( 16 _subs, 17 _SeqStr, 18 _FontSize, 19 _SeqArray, 20 _SeqFloat, 21 Palette_t, 22 _Normalize, 23 _CoordTuple, 24 _FontWeight, 25 _plot_edges, 26 _AvailShapes, 27 _set_outline, 28 _decorate_axs, 29 _plot_scatter, 30 _plot_segment, 31 _set_ax_title, 32 _set_coords_crops, 33 _prepare_args_plot, 34 _image_spatial_attrs, 35 _prepare_params_plot, 36 _set_color_source_vec, 37 ) 38 from squidpy._constants._constants import ScatterShape 39 from squidpy._constants._pkg_constants import Key File /public/home/yzhang/micromamba/envs/Spatial/lib/python3.9/site-packages/squidpy/pl/_spatial_utils.py:35 32 import dask.array as da 34 from matplotlib import colors, pyplot as plt, rcParams, patheffects ---> 35 from matplotlib.cm import get_cmap 36 from matplotlib.axes import Axes 37 from matplotlib.colors import ( 38 Colormap, 39 Normalize, (...) 42 ListedColormap, 43 ) ImportError: cannot import name 'get_cmap' from 'matplotlib.cm' (/home1/yzhang/.local/lib/python3.9/site-packages/matplotlib/cm.py) ```

Version

Python version: 3.9.19 | packaged by conda-forge | (main, Mar 20 2024, 12:50:21) squidpy version: 1.2.2

zyll123 commented 3 months ago

According to the import error, it seems that python import matplotlib from the global environment (/home1/yzhang/.local/lib/python3.9/site-packages/matplotlib/cm.py), although 'which python' shows i am using '/public/home/yzhang/micromamba/envs/Spatial/bin/python'. The matplotlib under '/home1/yzhang/.local/lib/python3.9/site-packages/matplotlib/cm.py' is indeed version 3.9.0. Is it a issue about the import path?

giovp commented 3 months ago

the problem is the squidpy version, I would install the latest squidpy version in your environment

zyll123 commented 3 months ago

I solved this by checking

import sys
for path in sys.path:
    print(path)

and I found that the path for global environment was ahead of path for micromamba environment:

/public/home/yzhang/micromamba/envs/Spatial/lib/python39.zip
/public/home/yzhang/micromamba/envs/Spatial/lib/python3.9
/public/home/yzhang/micromamba/envs/Spatial/lib/python3.9/lib-dynload

/home1/yzhang/.local/lib/python3.9/site-packages
/public/home/yzhang/micromamba/envs/Spatial/lib/python3.9/site-packages

So I changed the sys path to put micromamba environment ahead of global one:

env_site_packages = '/public/home/yzhang/micromamba/envs/Spatial/lib/python3.9/site-packages'
user_site_packages = '/home1/yzhang/.local/lib/python3.9/site-packages'
sys.path.remove(env_site_packages)
sys.path.insert(sys.path.index(user_site_packages), env_site_packages)

for path in sys.path:
    print(path)

Now the import works well