python 2.7 functools_lru_cache虽然安装但不导入

Mat*_*ath 30 python backport matplotlib

当我尝试导入matplotlib时出现错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py", line 128, in <module>
  from matplotlib.rcsetup import defaultParams, validate_backend, cycler
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/rcsetup.py", line 29, in <module>
    from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
  File "/usr/local/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
Run Code Online (Sandbox Code Playgroud)

backports本身导入正常.当我尝试手动安装functools时

sudo pip install backports.functools_lru_cache
Run Code Online (Sandbox Code Playgroud)

我得到的信息

Requirement already satisfied: backports.functools_lru_cache in /usr/local/lib/python2.7/dist-packages
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试

import backports.functools_lru_cache
Run Code Online (Sandbox Code Playgroud)

我得到的信息

ImportError: No module named functools_lru_cache
Run Code Online (Sandbox Code Playgroud)

系统信息Ubuntu 16 Python 2.7.12 Pip 9.0.1

小智 37

如果有人仍然遇到这个问题并重新安装backports.functools_lru_cache在他的情况下不起作用,就像在我的情况下那样,那么可能安装旧版本的matplotlib会起作用.例如:

pip install matplotlib==2.0.2
Run Code Online (Sandbox Code Playgroud)

版本2.2.0出现问题,我切换到2.0.2,现在正在运行.我没有检查其他版本.

  • 将Ubuntu更新到18.04后,我遇到了同样的问题。这个答案为我解决了。注意:在通过pip之前,我还必须卸载matplotlib:`pip uninstall matplotlib` (3认同)

Shr*_*aju 22

我有同样的问题,但我修复了它.

首先卸载

pip uninstall backports.functools_lru_cache
Run Code Online (Sandbox Code Playgroud)

然后重新安装它.

pip install backports.functools_lru_cache
Run Code Online (Sandbox Code Playgroud)

现在我可以导入matplotlib了.希望这可以帮助.

  • 我遇到了同样的问题.卸载/安装backports.functools_lru_cache没有帮助.我正在使用Ubuntu 16.04,Python 27,pip 9.0.1) (3认同)

小智 14

安装箭头使用:

pip install arrow==0.12.0 
Run Code Online (Sandbox Code Playgroud)

为我解决了这个问题


eno*_*ufs 12

pip命令实际上是pip3,当我使用python(2.7)时发生了"ImportError".

pip2 uninstall backports.functools_lru_cache
Run Code Online (Sandbox Code Playgroud)

然后,

pip2 install backports.functools_lru_cache
Run Code Online (Sandbox Code Playgroud)

解决了我的问题.


小智 7

你必须检查backports的导入路径是什么:

import backports
print('Backports Path: {0}'.format(backports.__path__))
Run Code Online (Sandbox Code Playgroud)

1.导入路径是主要的python路径(Matimath问题的情况)

pip uninstall backports.functools_lru_cache   (this will uninstall it from /usr/local/)
pip install backports.functools_lru_cache

2.导入路径是本地usr目录(〜/ .local /,或%APPDATA%\ Python for windows)

pip uninstall backports.functools_lru_cache 
pip install --user backports.functools_lru_cache

对python2 使用pip2命令.

这种不一致的原因是,在另一个模块/软件包安装过程中,backports软件包的导入路径可能已经更改(例如,来自backports.configparser模块) - 有关更多详细信息,请参阅此处:https: //bugs.python.org/issue31741


JDQ*_*JDQ 6

Following from Aditya Jain's answer,

[python -m] pip uninstall backports.functools_lru_cache
[python -m] pip install backports.functools_lru_cache==1.2.1
Run Code Online (Sandbox Code Playgroud)

which will avoid installing arrow merely to downgrade functools_lru_cache.