Search code examples
condafailed-installationscanpy

Why I have an Error when importing scanpy._compat?


Hi I'm new here so sorry for my language which could result inaccurate. I'm working with a conda environment in which I installed scanpy. However, when I import the module scanpy._compat, it returns an import error:

from scanpy._compat import Literal

ModuleNotFoundError: No module named 'scanpy._compat'

I tried to upgrade scanpy: pip install --upgrade scanpy and I also created another conda environment following installation steps present on scanpy github (https://github.com/theislab/scanpy/blob/master/docs/installation.rst). In each of the last two cases everything works well except for the import of scanpy._compat module.

Someone knows if I'm doing something wrong here?


Solution

  • In the _compat.py you will note that the following code is provided:

    First:

    try:
        from typing import Literal
    
    

    And for the exception:

    except ImportError:
        try:
            from typing_extensions import Literal
        except ImportError:
    
            class LiteralMeta(type):
                def __getitem__(cls, values):
                    if not isinstance(values, tuple):
                        values = (values,)
                    return type('Literal_', (Literal,), dict(__args__=values))
    
            class Literal(metaclass=LiteralMeta):
                pass
    

    Consequently, when you are trying to import Literal, you could just as well circumvent the question by simply importing typing and do typing.Literal (python version >3.8 ) for whatever the situation you wanted to apply the code for.

    However, the question suggests that scanpy cant be imported at all. Ensure that after installing the environment that you also activate it:

    $ conda info --envs to find out the names of the environments you have available $ conda activate [relevant env name] to activate the relevant conda environment.

    To then try the code again:

    $ python to enter the pythoninterpreter

    > from scanpy._compat import Literal to test whether the import works.