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

Python导入matplotlib报错缺少backports.functools_lru_cache但IPython正常

Ubuntu 18.04下Python导入matplotlib报错:找不到backports.functools_lru_cache

问题背景

我在Ubuntu 18.04系统中使用Python 2.7.15rc1时碰到了一个棘手的问题:

  • 直接通过python命令启动交互环境,导入matplotlib时会抛出ImportError,提示找不到backports.functools_lru_cache模块
  • 但用IPython导入matplotlib却完全正常,而且已经确认IPython和python命令使用的是同一个Python版本

我尝试过多种排查和修复手段:

  • 清理了Python和IPython的路径配置
  • 多次卸载并重装matplotlib与backports.functools_lru_cache,不管是用系统的apt还是pip,每次重装前都会彻底卸载旧版本
  • 试过Stack Exchange及GitHub上的多种解决方案,但都没能解决问题

报错详情

Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 127, in <module>
    from matplotlib.rcsetup import defaultParams, validate_backend, cycler
  File "/usr/lib/python2.7/dist-packages/matplotlib/rcsetup.py", line 29, in <module>
    from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
  File "/usr/lib/python2.7/dist-packages/matplotlib/fontconfig_pattern.py", line 32, in <module>
    from backports.functools_lru_cache import lru_cache
ImportError: No module named functools_lru_cache

IPython中导入matplotlib无异常,此处省略相关输出。

最终解决方法

经过多次尝试后,我通过指定安装matplotlib的特定版本解决了问题,执行以下命令:

pip install matplotlib==2.0.2

安装完成后,无论是直接用python命令还是IPython,都能正常加载matplotlib了。

内容的提问来源于stack exchange,提问作者Chad Sexington

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:56:06