ImportError: cannot import name cbook

前端 未结 5 1077
星月不相逢
星月不相逢 2020-12-07 00:39
>>> import matplotlib
Traceback (most recent call last):
  File \"\", line 1, in 
  File \"/usr/local/lib/python2.7/dist-packages         


        
相关标签:
5条回答
  • 2020-12-07 00:56

    just sharing my experience, I was trying to run Python code (2.7) making use of matplotlib. The code is running inside a docker container based on a Debian 10 image.

    In the end I based the solution on the answers here, but there was a small caveat. I had to do the actions in the following order:

    1. Install the rest of the requirements
    2. Install matplotlib (no need to specify a version in my case, 2.2.5 was installed by pip)
    3. Install arrow
    4. Uninstall backports.functools_lru_cache
    5. Install backports.functools_lru_cache version 1.2.1

    The code on the dockerfile is as it follows:

    RUN pip2 install -r requirements.txt
    RUN pip2 install matplotlib
    RUN pip2 install arrow
    RUN pip2 uninstall -y backports.functools_lru_cache
    RUN pip2 install backports.functools_lru_cache==1.2.1
    

    You can find the whole project on github: https://github.com/n3if/irassh.git

    0 讨论(0)
  • 2020-12-07 00:59

    My experience is to be careful about the version of matplotlib. Today the latest version is 3.3 and has this issue. So I specify the previous version that worked for me:

    pip install matplotlib==3.2.2
    
    0 讨论(0)
  • 2020-12-07 01:07

    I hit this issue today due to a bad dependency.

    If you have both backports.shutil_get_terminal_size and backports.functools_lru_cache installed, you can encounter this.

    Matplotlib has a brittle workaround for a cyclic import:

    # cbook must import matplotlib only within function
    # definitions, so it is safe to import from it here.
    from . import cbook
    

    Until PR #10483, matplotlib dependended on backports.functools_lru_cache.

    However, ipython depends on backports.shutil_get_terminal_size, and that package doesn't set up a namespace package properly.

    If you have this problem, you'll see these symptoms:

    >>> import backports
    <module 'backports.shutil_get_terminal_size' from '/Users/whughes/miniconda2/envs/scratch/lib/python2.7/site-packages/backports/shutil_get_terminal_size/__init__.pyc'>
    >>> >import backports.functools_lru_cache
    ImportError: No module named functools_lru_cache
    

    The problem with backports.shutil_get_terminal_size is that it doesn't define a namespace package, so it breaks any other backports.foo packages.

    Reinstalling matplotlib fixes this because it changes the order in sys.path, putting backports.functools_lru_cache first, and that package defines a proper namespace.

    You can also fix this by reinstalling backports.shutil_get_terminal_size.

    0 讨论(0)
  • 2020-12-07 01:09

    I solve the problem uninstalling matplotpli and reinstalling without pip:

    $ sudo apt-get install python-matplotlib
    

    Thanks to this README.html.

    0 讨论(0)
  • 2020-12-07 01:18

    1. Try to update matplotlib

    python -m pip install -U matplotlib
    

    2. Try to reinstall matplotlib

    python -m pip uninstall matplotlib
    python -m pip install -U matplotlib
    

    What does the following snippet print to the console?

    python -c "import matplotlib"
    
    0 讨论(0)
提交回复
热议问题