Python 导入错误:无法导入名称 cbook

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44623376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:15:37  来源:igfitidea点击:

ImportError: cannot import name cbook

pythonmatplotlib

提问by Anas Mubarak

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

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

回答by Szabolcs Dombi

1.Try to update matplotlib

1.尝试更新matplotlib

python -m pip install -U matplotlib

2.Try to reinstall matplotlib

2.尝试重新安装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"

回答by Wilfred Hughes

I hit this issue today due to a bad dependency.

由于依赖性不好,我今天遇到了这个问题。

If you have both backports.shutil_get_terminal_sizeand backports.functools_lru_cacheinstalled, you can encounter this.

如果你同时backports.shutil_get_terminal_sizebackports.functools_lru_cache安装,你可以遇到这种情况。

Matplotlib has a brittle workaround for a cyclic import:

Matplotlib 对循环导入有一个脆弱的解决方法:

# 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.

PR #10483 之前,matplotlib 依赖于backports.functools_lru_cache.

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

但是,ipython 依赖于backports.shutil_get_terminal_size,并且 该包没有正确设置命名空间包

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_sizeis that it doesn't define a namespace package, so it breaks any other backports.foopackages.

问题backports.shutil_get_terminal_size在于它没有定义命名空间包,所以它破坏了任何其他 backports.foo

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

重新安装 matplotlib 解决了这个问题,因为它改变了 中的顺序 sys.pathbackports.functools_lru_cache放在第一位,并且该包定义了一个适当的命名空间。

You can also fix this by reinstalling backports.shutil_get_terminal_size.

您也可以通过重新安装来解决此问题backports.shutil_get_terminal_size

回答by user2078500

I solve the problem uninstalling matplotpli and reinstalling without pip:

我解决了卸载 matplotpli 并在没有pip 的情况下重新安装的问题:

$ sudo apt-get install python-matplotlib

Thanks to this README.html.

感谢这个 README.html