Python,matplotlib 加载使用 ipython 但不使用 python

Python, matplotlib loads using ipython but not using python

当我尝试使用 python 加载 matplotlib 时,我收到关于加载 backports.functools_lru_cache 模块的错误。当我尝试使用 ipython 加载 matplotlib 时,它加载得很好。

据我所知,ipython 使用与调用 python 使用的 python 相同的版本。我经历了 python 和 ipython 都使用并卸载了 matplotlib 和 backports.functools_lru_cache 的路径。我试过然后使用 apt 和 pip 重新安装,总是在尝试下一次之前卸载上一次尝试。

我经历了一堆 stackexchange 解决方案并尝试了一些 github 解决方案但没有任何运气。我 运行 Ubuntu 18.04.

错误:

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,因为它加载得很好。

更新:

如果我安装使用:

pip install matplotlib==2.0.2

matplotlib 适用于 python 和 ipython。

问题似乎是路径问题,我不确定如何解决。我在 Ubuntu 18.04 上使用 apt 将系统模块安装到 /usr/lib/python2.7/dist-packages/ 中。此目录是 backports.functools_lru_cache 所在的目录。我还在 /usr/local/lib/python2.7/dist-packages/ 中安装了一些编译模块,其中有一个没有 functools_lru_cache 的向后移植模块。问题是 python 没有遵循系统路径,似乎只是在 /usr/local/lib/python2.7/dist-packages/ 中寻找向后移植。但是,ipython 会同时查看两者,这就是为什么 ipython 有效而 python 无效的原因。

python:

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 sys
>>> sys.path
['', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/brendan/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0']
>>> import backports
>>> backports.__path__
['/usr/local/lib/python2.7/dist-packages/backports']

ipython:

Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: sys.path
Out[1]: 
['',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/home/brendan/.local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/gtk-2.0']

In [2]: import backports

In [3]: backports.__path__
Out[3]: 
['/usr/local/lib/python2.7/dist-packages/backports',
 '/usr/lib/python2.7/dist-packages/backports']

解法:

为了解决这个问题,我手动将functools_lru_cache.py从/usr/lib/python2.7/dist-packages/backports/复制到/usr/local/lib/python2.7/dist-packages/backports/。现在我可以 运行 matplotlib 2.2.2 在 python 和 ipython.

中没问题

我要把这个标记为已回答,因为我的问题现在围绕着 python 和路径。