【问题标题】:Python, matplotlib loads using ipython but not using pythonPython,matplotlib 加载使用 ipython 但不使用 python
【发布时间】:2018-05-26 00:35:49
【问题描述】:

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

据我所知,ipython 使用的 python 版本与调用 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。

【问题讨论】:

    标签: python matplotlib ubuntu-18.04


    【解决方案1】:

    问题似乎是路径问题,我不知道如何解决。我在 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 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/。现在我可以在 python 和 ipython 中运行 matplotlib 2.2.2 没问题了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 2017-03-18
      • 2015-11-01
      • 2018-01-23
      • 1970-01-01
      相关资源
      最近更新 更多