You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

全新安装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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:06:27