23
>>> import matplotlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py", line 123, in <module>
    from . import cbook
ImportError: cannot import name cbook

I didn't find a solution, can anyone help?

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
Anas Mubarak
  • 289
  • 1
  • 3
  • 9
  • 1
    I was able to fix this issue using this https://stackoverflow.com/questions/46651581/importerror-cannot-import-name-cbook-when-using-pycharms-profiler – richar8086 Dec 08 '17 at 17:55
  • try fixing cbook, https://stackoverflow.com/questions/44623376/importerror-cannot-import-name-cbook – zabop Sep 09 '18 at 19:09

7 Answers7

23

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"
Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71
  • 2
    I have this import error too. I am using conda package manager. I tried uninstalling and installing and that didn't solve it. `python -c "import matplotlib"` produces `ImportError: Matplotlib requires six>=1.10; you have 1.9.0`. Then `conda update six` solved the problem. Thanks for the advice. – Bill Sep 17 '18 at 06:12
  • 2
    I have the same issue using matplotlib. I have tried reinstalling matplotlib and ran `python -c "import matplotlib"` produces `ImportError: No module named functools_lru_cache` – Krunal Sonparate Oct 07 '18 at 17:22
  • 2
    @KrunalSonparate I solved the *functools_lru_cache_ issue* by installing the *functools32* package ( `pip install functools32` ). – gekaklam Oct 12 '18 at 09:35
  • 1
    @G.Kaklam.: I am still facing the same problem even after `pip install functools32` although functools32 is successfully installed. – Lot_to_learn Oct 29 '18 at 11:34
8

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.

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
  • @Wlifred : I tried your way and I am still facing the porblem of `cbook` means reinstalling backports.shutil_get_terminal_size` or `backports` or `functools32` etc. Nothing is working. Would you suggest me any other ideas...Thanks – Lot_to_learn Oct 29 '18 at 11:38
  • 3
    Install matplotlib 2.0.0 using `pip install matplotlib==2.0.2` as per the answer posted [here] (https://stackoverflow.com/a/49335954/7488168) will resolve the error – Krunal Sonparate Dec 23 '18 at 13:19
4

I solve the problem uninstalling matplotpli and reinstalling without pip:

$ sudo apt-get install python-matplotlib

Thanks to this README.html.

Milo
  • 3,365
  • 9
  • 30
  • 44
3

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
Yuchao Jiang
  • 3,522
  • 30
  • 23
1

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

neif
  • 480
  • 7
  • 13
0

Go to /usr/local/lib/python2.7/dist-packages/matplotlib and delete __init__.py if there are more than one of those files, delete all of them... It will work. For your confidence save them somewhere else first :) see your error message, the directory is shown, the file is the init constructor written there

Marzi Heidari
  • 2,660
  • 4
  • 25
  • 57
-1

working...

pip install --user matplotlib==2.0.2
Abhijith M
  • 743
  • 5
  • 5