全新安装matplotlib后出现cbook缺失错误,如何解决?
Fixing "ImportError: cannot import name 'cbook'" When Importing Matplotlib.pyplot
我之前也碰到过一模一样的问题,这个错误基本是因为matplotlib安装文件损坏,或者版本和你的Python 3.6不兼容导致的——毕竟Python 3.6已经比较老旧,新的matplotlib版本已经不再支持它了。试试下面的步骤来修复:
步骤1:彻底卸载现有的matplotlib
首先要把损坏的安装包完全清理掉:
pip uninstall -y matplotlib
如果卸载后还有残留文件(这很常见),手动删除用户目录下的matplotlib文件夹:
rm -rf ~/.local/lib/python3.6/site-packages/matplotlib
步骤2:清理pip缓存
避免安装时使用损坏的缓存文件:
pip cache purge
步骤3:安装兼容版本的matplotlib
Python 3.6最高支持matplotlib 3.3.x版本,我们安装这个分支的最后一个稳定版:
pip install matplotlib==3.3.4
可选:检查IPython版本兼容性
如果修复后还是有问题,可以把IPython更新到Python 3.6支持的最高版本(7.16.1),确保和matplotlib兼容:
pip install ipython==7.16.1
你的错误信息参考
bash-4.2$ ipython Python 3.6.3 (default, Jan 4 2018, 16:40:53) Type 'copyright', 'credits' or 'license' for more information IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import matplotlib.pyplot as plt --------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-4-a0d2faabd9e9> in <module>() ----> 1 import matplotlib.pyplot as plt ~/.local/lib/python3.6/site-packages/matplotlib/__init__.py in <module>() 125 # cbook must import matplotlib only within function 126 # definitions, so it is safe to import from it here. --> 127 from . import cbook 128 from matplotlib.cbook import ( 129 _backports, mplDeprecation, dedent, get_label, sanitize_sequence) ImportError: cannot import name 'cbook'
内容的提问来源于stack exchange,提问作者steffen
相关产品推荐
相关产品推荐

