python 2.7 functools_lru_cache 虽然安装了但不导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47179433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:03:22  来源:igfitidea点击:

python 2.7 functools_lru_cache does not import although installed

pythonmatplotlibbackport

提问by Matimath

When I try to import matplotlib I get an error

当我尝试导入 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

backports itself imports properly. When I try to install functools manually via

backports 本身正确导入。当我尝试通过以下方式手动安装 functools 时

sudo pip install backports.functools_lru_cache

I get info that

我得到的信息是

Requirement already satisfied: backports.functools_lru_cache in /usr/local/lib/python2.7/dist-packages

Nevertheless when i try to

尽管如此,当我尝试

import backports.functools_lru_cache

I get info that

我得到的信息是

ImportError: No module named functools_lru_cache

System info Ubuntu 16 Python 2.7.12 Pip 9.0.1

系统信息 Ubuntu 16 Python 2.7.12 Pip 9.0.1

回答by Kamil Smolak

If someone is still having that problem and reinstalling backports.functools_lru_cache do not work in his case, as it was in my case, then probably installing older version of matplotlib would work. For example:

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

pip install matplotlib==2.0.2

Problem occurred for version 2.2.0, I switched to 2.0.2 and it is working now. I did not check other versions.

2.2.0 版出现问题,我切换到 2.0.2 版,现在可以使用了。我没有检查其他版本。

回答by Shree Ranga Raju

I had the same problem but I fixed it.

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

Uninstall first

先卸载

pip uninstall backports.functools_lru_cache

and then re-install it.

然后重新安装它。

pip install backports.functools_lru_cache

Now I'm able to import matplotlib. Hope this helps.

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

回答by Aditya Jain

Install arrow using:

使用以下方法安装箭头:

pip install arrow==0.12.0 

fixed this issue for me

为我解决了这个问题

回答by enobufs

The pip command was actually the pip3, and the "ImportError" was happening when I used python (2.7).

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

pip2 uninstall backports.functools_lru_cache

then,

然后,

pip2 install backports.functools_lru_cache

fixed my problem.

解决了我的问题。

回答by Charidimos

You have to check what is the import pathof backportspackage:

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

import backports
print('Backports Path: {0}'.format(backports.__path__))

1. The import path is the main python path( the case of Matimath's question)

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

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

2. The import path is the local usr dir(~/.local/, or %APPDATA%\Python for windows)

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

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

Use pip2command for python2.

对 python2使用pip2命令。

The reason for this inconsistency is that the import path of backports package might have been changed during another module/package installation (eg. from backports.configparser module) - see here for more details: https://bugs.python.org/issue31741

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

回答by HilmiK

I had the same problem and my solution was;

我遇到了同样的问题,我的解决方案是;

回答by redsphinx

I solved my problem by removing the excessive matplotlib packages. I found out that when importing matplotlib it was attempting to import backports.functools_lru_cacheand there it was throwing the Importerror.

我通过删除过多的 matplotlib 包解决了我的问题。我发现在导入 matplotlib 时它正在尝试,import backports.functools_lru_cache并且在那里抛出Importerror.

I realized that I had different matplotlib packages in many locations:

我意识到我在很多地方都有不同的 matplotlib 包:

/usr/lib/python2.7/dist-packages/matplotlib/
/usr/lib/python2.7/site-packages/matplotlib/

I removed the site-packages one. I left the dist-packages one intact.

我删除了站点包之一。我将 dist-packages 完好无损地保留了下来。

Then I ran the following commands in python:

然后我在python中运行了以下命令:

matplotlib.get_configdir()
matplotlib.get_cachedir()

and I removed the matplotlib packages in the output paths of these commands.

我删除了这些命令的输出路径中的 matplotlib 包。

Then I removed the matplotlib in my virtualenvironment:

然后我在我的虚拟环境中删除了 matplotlib:

mv /home/username/virtualenvironment/lib/python2.7/matplotlib* /tmp

Finally I removed the one in the .local folder:

最后我删除了 .local 文件夹中的那个:

mv /home/username/.local/lib/python2.7/matplotlib* /tmp

Now importing matplotlib works fine. So when I run in python:

现在导入 matplotlib 工作正常。所以当我在 python 中运行时:

matplotlib.__file__

it returns

它返回

'/usr/lib/python2.7/dist-packages/matplotlib/__init__.pyc'

Now it does not throw error anymore when import backports.functools_lru_cache

现在它不再抛出错误 import backports.functools_lru_cache

回答by darrey

You are using pyhton 2. try to use pip2 instead:

您正在使用 pyhton 2. 尝试使用 pip2 代替:

  • pip2 uninstall matplotlib
  • sudo apt-get autoremove python-matplotlib
  • sudo apt-get install python-matplotlib
  • pip2 卸载 matplotlib
  • sudo apt-get autoremove python-matplotlib
  • 须藤 apt-get 安装 python-matplotlib

回答by JDQ

Following from Aditya Jain's answer,

根据Aditya Jain 的回答

[python -m] pip uninstall backports.functools_lru_cache
[python -m] pip install backports.functools_lru_cache==1.2.1

which will avoid installing arrowmerely to downgrade functools_lru_cache.

这将避免安装arrow只是为了降级functools_lru_cache

回答by Ragesh Moorkoth

I had same issue, re-installation of backports.functools_lru_cache resolved the issue

我有同样的问题,重新安装 backports.functools_lru_cache 解决了这个问题