ImportError:无法导入名称cbook

Ana*_*rak 16 python matplotlib

>>> 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
Run Code Online (Sandbox Code Playgroud)

我找不到解决方案,有人可以帮忙吗?

Sza*_*mbi 18

1.尝试更新matplotlib

python -m pip install -U matplotlib
Run Code Online (Sandbox Code Playgroud)

2.尝试重新安装matplotlib

python -m pip uninstall matplotlib
python -m pip install -U matplotlib
Run Code Online (Sandbox Code Playgroud)

以下代码段打印到控制台的内容是什么?

python -c "import matplotlib"
Run Code Online (Sandbox Code Playgroud)

  • 我也有这个导入错误.我正在使用conda包管理器.我尝试卸载和安装,但没有解决它.`python -c"import matplotlib"`产生`ImportError:Matplotlib需要6> = 1.10; 你有1.9.0`.然后`conda update six`解决了这个问题.感谢您的建议. (2认同)
  • 我在使用 matplotlib 时遇到了同样的问题。我尝试重新安装 matplotlib 并运行 `python -c "import matplotlib"` 产生 `ImportError: No module named functools_lru_cache` (2认同)
  • @KrunalSonparate我通过安装*functools32*包(`pip install functools32`)解决了*functools_lru_cache_问题*. (2认同)

Wil*_*hes 7

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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.

  • 按照[此处](/sf/answers/3453516811/)上发布的答案,使用`pip install matplotlib == 2.0.2`安装matplotlib 2.0.0将解决该错误 (3认同)