scverse / scanpy-tutorials

Scanpy Tutorials.
https://scanpy-tutorials.readthedocs.io/
189 stars 117 forks source link

ImportError: Please install the leiden algorithm: `conda install -c conda-forge leidenalg` or `pip3 install leidenalg`. #76

Closed Aspirinnnn closed 1 year ago

Aspirinnnn commented 1 year ago

Hi, I'm running the pbmc3k tutorial on Jupyter Notebook. When I ran the code sc.tl.leiden(adata) , I encountered this error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
File ~/anaconda3/envs/env4/lib/python3.9/site-packages/scanpy/tools/_leiden.py:108, in leiden(adata, resolution, restrict_to, random_state, key_added, adjacency, directed, use_weights, n_iterations, partition_type, neighbors_key, obsp, copy, **partition_kwargs)
    107 try:
--> 108     import leidenalg
    109 except ImportError:

File ~/anaconda3/envs/env4/lib/python3.9/site-packages/leidenalg/__init__.py:35
      2 r""" This package implements the Leiden algorithm in ``C++`` and exposes it to
      3 python.  It relies on ``(python-)igraph`` for it to function. Besides the
      4 relative flexibility of the implementation, it also scales well, and can be run
   (...)
     33 not immediately available in :func:`leidenalg.find_partition`.
     34 """
---> 35 from .functions import ALL_COMMS
     36 from .functions import ALL_NEIGH_COMMS

File ~/anaconda3/envs/env4/lib/python3.9/site-packages/leidenalg/functions.py:2
      1 import sys
----> 2 import igraph as _ig
      3 from . import _c_leiden

File ~/anaconda3/envs/env4/lib/python3.9/site-packages/igraph/__init__.py:25
      6 __license__ = """
      7 Copyright (C) 2006- The igraph development team
      8 
   (...)
     22 02110-1301 USA
     23 """
---> 25 from igraph._igraph import (
     26     ADJ_DIRECTED,
     27     ADJ_LOWER,
     28     ADJ_MAX,
     29     ADJ_MIN,
     30     ADJ_PLUS,
     31     ADJ_UNDIRECTED,
     32     ADJ_UPPER,
     33     ALL,
     34     ARPACKOptions,
     35     BFSIter,
     36     BLISS_F,
     37     BLISS_FL,
     38     BLISS_FLM,
     39     BLISS_FM,
     40     BLISS_FS,
     41     BLISS_FSM,
     42     DFSIter,
     43     Edge,
     44     GET_ADJACENCY_BOTH,
     45     GET_ADJACENCY_LOWER,
     46     GET_ADJACENCY_UPPER,
     47     GraphBase,
     48     IN,
     49     InternalError,
     50     OUT,
     51     REWIRING_SIMPLE,
     52     REWIRING_SIMPLE_LOOPS,
     53     STAR_IN,
     54     STAR_MUTUAL,
     55     STAR_OUT,
     56     STAR_UNDIRECTED,
     57     STRONG,
     58     TRANSITIVITY_NAN,
     59     TRANSITIVITY_ZERO,
     60     TREE_IN,
     61     TREE_OUT,
     62     TREE_UNDIRECTED,
     63     Vertex,
     64     WEAK,
     65     arpack_options as default_arpack_options,
     66     community_to_membership,
     67     convex_hull,
     68     is_bigraphical,
     69     is_degree_sequence,
     70     is_graphical,
     71     is_graphical_degree_sequence,
     72     set_progress_handler,
     73     set_random_number_generator,
     74     set_status_handler,
     75     umap_compute_weights,
     76     __igraph_version__,
     77 )
     78 from igraph.adjacency import (
     79     _get_adjacency,
     80     _get_adjacency_sparse,
   (...)
     83     _get_inclist,
     84 )

ImportError: dlopen(/Users/cailinghui/anaconda3/envs/env4/lib/python3.9/site-packages/igraph/_igraph.abi3.so, 0x0002): Library not loaded: @rpath/libblas.3.dylib
  Referenced from: /Users/cailinghui/anaconda3/envs/env4/lib/python3.9/site-packages/igraph/_igraph.abi3.so
  Reason: tried: '/Users/cailinghui/anaconda3/envs/env4/lib/python3.9/site-packages/igraph/../../../libblas.3.dylib' (no such file), '/Users/cailinghui/anaconda3/envs/env4/lib/python3.9/site-packages/igraph/../../../libblas.3.dylib' (no such file), '/Users/cailinghui/anaconda3/envs/env4/bin/../lib/libblas.3.dylib' (no such file), '/Users/cailinghui/anaconda3/envs/env4/bin/../lib/libblas.3.dylib' (no such file), '/usr/local/lib/libblas.3.dylib' (no such file), '/usr/lib/libblas.3.dylib' (no such file)

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
Cell In[60], line 1
----> 1 sc.tl.leiden(adata)

File ~/anaconda3/envs/env4/lib/python3.9/site-packages/scanpy/tools/_leiden.py:110, in leiden(adata, resolution, restrict_to, random_state, key_added, adjacency, directed, use_weights, n_iterations, partition_type, neighbors_key, obsp, copy, **partition_kwargs)
    108     import leidenalg
    109 except ImportError:
--> 110     raise ImportError(
    111         'Please install the leiden algorithm: `conda install -c conda-forge leidenalg` or `pip3 install leidenalg`.'
    112     )
    113 partition_kwargs = dict(partition_kwargs)
    115 start = logg.info('running Leiden clustering')

ImportError: Please install the leiden algorithm: `conda install -c conda-forge leidenalg` or `pip3 install leidenalg`.

Then I tried import leidenalg but still came to error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[61], line 1
----> 1 import leidenalg

File ~/anaconda3/envs/env4/lib/python3.9/site-packages/leidenalg/__init__.py:35
      1 # -*- coding: utf-8 -*-
      2 r""" This package implements the Leiden algorithm in ``C++`` and exposes it to
      3 python.  It relies on ``(python-)igraph`` for it to function. Besides the
      4 relative flexibility of the implementation, it also scales well, and can be run
   (...)
     33 not immediately available in :func:`leidenalg.find_partition`.
     34 """
---> 35 from .functions import ALL_COMMS
     36 from .functions import ALL_NEIGH_COMMS
     37 from .functions import RAND_COMM

File ~/anaconda3/envs/env4/lib/python3.9/site-packages/leidenalg/functions.py:2
      1 import sys
----> 2 import igraph as _ig
      3 from . import _c_leiden
      4 from ._c_leiden import ALL_COMMS

File ~/anaconda3/envs/env4/lib/python3.9/site-packages/igraph/__init__.py:25
      1 """
      2 igraph library.
      3 """
      6 __license__ = """
      7 Copyright (C) 2006- The igraph development team
      8 
   (...)
     22 02110-1301 USA
     23 """
---> 25 from igraph._igraph import (
     26     ADJ_DIRECTED,
     27     ADJ_LOWER,
     28     ADJ_MAX,
     29     ADJ_MIN,
     30     ADJ_PLUS,
     31     ADJ_UNDIRECTED,
     32     ADJ_UPPER,
     33     ALL,
     34     ARPACKOptions,
     35     BFSIter,
     36     BLISS_F,
     37     BLISS_FL,
     38     BLISS_FLM,
     39     BLISS_FM,
     40     BLISS_FS,
     41     BLISS_FSM,
     42     DFSIter,
     43     Edge,
     44     GET_ADJACENCY_BOTH,
     45     GET_ADJACENCY_LOWER,
     46     GET_ADJACENCY_UPPER,
     47     GraphBase,
     48     IN,
     49     InternalError,
     50     OUT,
     51     REWIRING_SIMPLE,
     52     REWIRING_SIMPLE_LOOPS,
     53     STAR_IN,
     54     STAR_MUTUAL,
     55     STAR_OUT,
     56     STAR_UNDIRECTED,
     57     STRONG,
     58     TRANSITIVITY_NAN,
     59     TRANSITIVITY_ZERO,
     60     TREE_IN,
     61     TREE_OUT,
     62     TREE_UNDIRECTED,
     63     Vertex,
     64     WEAK,
     65     arpack_options as default_arpack_options,
     66     community_to_membership,
     67     convex_hull,
     68     is_bigraphical,
     69     is_degree_sequence,
     70     is_graphical,
     71     is_graphical_degree_sequence,
     72     set_progress_handler,
     73     set_random_number_generator,
     74     set_status_handler,
     75     umap_compute_weights,
     76     __igraph_version__,
     77 )
     78 from igraph.adjacency import (
     79     _get_adjacency,
     80     _get_adjacency_sparse,
   (...)
     83     _get_inclist,
     84 )
     85 from igraph.automorphisms import (
     86     _count_automorphisms_vf2,
     87     _get_automorphisms_vf2,
     88 )

ImportError: dlopen(/Users/cailinghui/anaconda3/envs/env4/lib/python3.9/site-packages/igraph/_igraph.abi3.so, 0x0002): Library not loaded: @rpath/libblas.3.dylib
  Referenced from: /Users/cailinghui/anaconda3/envs/env4/lib/python3.9/site-packages/igraph/_igraph.abi3.so
  Reason: tried: '/Users/cailinghui/anaconda3/envs/env4/lib/python3.9/site-packages/igraph/../../../libblas.3.dylib' (no such file), '/Users/cailinghui/anaconda3/envs/env4/lib/python3.9/site-packages/igraph/../../../libblas.3.dylib' (no such file), '/Users/cailinghui/anaconda3/envs/env4/bin/../lib/libblas.3.dylib' (no such file), '/Users/cailinghui/anaconda3/envs/env4/bin/../lib/libblas.3.dylib' (no such file), '/usr/local/lib/libblas.3.dylib' (no such file), '/usr/lib/libblas.3.dylib' (no such file)

I've installed the leidenalg package. How can I solve this? I am using Python 3.9.18, Jupyter Notebook 6.5.4, scanpy==1.9.5 anndata==0.10.0 umap==0.5.3 numpy==1.24.3 scipy==1.11.3 pandas==2.0.3 scikit-learn==1.3.0 statsmodels==0.14.0 pynndescent==0.5.10

Thanks a lot!

flying-sheep commented 1 year ago

Seems like your conda environment is borked. Did you install igraph using conda? It should have pulled in libblas as a dependency.

Try conda install -c conda-forge python-igraph and if it doesn’t work, delete your environment and start new by installing scanpy and python-igraph into a fresh environment. It will work.