Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name cbook

Tags:

>>> 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?

like image 375
Anas Mubarak Avatar asked Jun 19 '17 06:06

Anas Mubarak


2 Answers

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"
like image 83
Szabolcs Dombi Avatar answered Sep 22 '22 17:09

Szabolcs Dombi


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.

like image 31
Wilfred Hughes Avatar answered Sep 20 '22 17:09

Wilfred Hughes