pythonscanpy

ImportError: cannot import name 'stacked_violin' from 'scanpy.plotting._anndata'?


Until a couple of days ago, my code using the scanpy library worked fine, but last night it broke and threw me a new error

Traceback (most recent call last):
  File "./scanpy_normalization.py", line 4, in <module>
    import scanpy.api as sc
  File "/usr/local/lib/python3.8/site-packages/scanpy/api/__init__.py", line 27, in <module>
    from . import pl
  File "/usr/local/lib/python3.8/site-packages/scanpy/api/pl.py", line 1, in <module>
    from ..plotting._anndata import scatter, violin, ranking, clustermap, stacked_violin, heatmap, dotplot, matrixplot, tracksplot
ImportError: cannot import name 'stacked_violin' from 'scanpy.plotting._anndata' (/usr/local/lib/python3.8/site-packages/scanpy/plotting/_anndata.py

I import scanpy as

import scanpy.api as sc

and I update to the latest version of scanpy (if any) everytime I run the code. Was there a change in the last couple of days that I missed? Help is appreciated!


Solution

  • This is definitely a bug in the module and seems like its stemming from the latest version that you must have upgraded to.

    The error as it states

    cannot import name 'stacked_violin' from 'scanpy.plotting._anndata'

    and this is true since this _anndata.py it refers to does not contains that method. It is in fact in another file called stack_violin.py. See here for that function.

    To fix it you can simply take the hard way of overwriting the library so that the method is present in the file _anndata.py or you can simply revert back to the earlier version. There's also no need to update the library frequently unless there is a real need, and such error is often expected when elevating versions.