简体   繁体   English

Python,matplotlib使用ipython而不是使用python加载

[英]Python, matplotlib loads using ipython but not using python

When I try to load matplotlib using python I get an error about loading the backports.functools_lru_cache module. 当我尝试使用python加载matplotlib时,出现有关加载backports.functools_lru_cache模块的错误。 When I try to load matplotlib using ipython it loads just fine. 当我尝试使用ipython加载matplotlib时,它的加载就很好。

As near as I can tell ipython uses the same version of python that calling python uses. 据我所知,ipython使用的是与调用python相同的python版本。 I've gone through the path that both python and ipython use and uninstalled matplotlib and backports.functools_lru_cache. 我经历了python和ipython都使用并卸载matplotlib和backports.functools_lru_cache的路径。 I've tried then reinstalling using apt and pip, always uninstalling the previous attempt before trying the next. 我尝试过使用apt和pip重新安装,在尝试下一次尝试之前总是先卸载上一次尝试。

I've gone through a bunch of stackexchange solutions and tried some github solutions without any luck. 我经历了一堆stackexchange解决方案,并尝试了一些github解决方案,但没有任何运气。 I run Ubuntu 18.04. 我运行Ubuntu 18.04。

Error: 错误:

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

I won't show ipython, because it loads just fine. 我不会显示ipython,因为它加载得很好。

Update: 更新:

If I install using: 如果我使用以下方式安装:

pip install matplotlib==2.0.2

matplotlib works for both python and ipython. matplotlib适用于python和ipython。

The problem appears to be a path problem I am not sure how to solve. 该问题似乎是我不确定如何解决的路径问题。 I use apt on Ubuntu 18.04 to install system modules into /usr/lib/python2.7/dist-packages/. 我在Ubuntu 18.04上使用apt将系统模块安装到/usr/lib/python2.7/dist-packages/中。 This directory is where backports.functools_lru_cache lives. 该目录是backports.functools_lru_cache所在的目录。 I also have some compiled modules installed in /usr/local/lib/python2.7/dist-packages/ where there is a backports module WITHOUT functools_lru_cache. 我还在/usr/local/lib/python2.7/dist-packages/中安装了一些编译模块,其中有一个没有functools_lru_cache的反向端口模块。 The problem is that python is not following the system path and only seems to be looking in /usr/local/lib/python2.7/dist-packages/ for backports. 问题是python没有遵循系统路径,而似乎只在/usr/local/lib/python2.7/dist-packages/中寻找反向移植。 However, ipython looks in both which is why ipython was working while python was not. 但是,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: 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']

Solution: 解:

To solve the problem I manually copied functools_lru_cache.py from /usr/lib/python2.7/dist-packages/backports/ to /usr/local/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/。 Now I can run matplotlib 2.2.2 no problem in python and ipython. 现在我可以在python和ipython中运行matplotlib 2.2.2了。

I'm going to mark this answered because my question now revolves around python and path. 我将标记为已回答,因为我的问题现在围绕python和path展开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM