python 2.7 functools_lru_cache does not import alt

2019-01-14 15:40发布

When I try to import matplotlib I get an error

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

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

10条回答
唯我独甜
2楼-- · 2019-01-14 15:46

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_cache and there it was throwing the Importerror.

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

/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.

Then I ran the following commands in python:

matplotlib.get_configdir()
matplotlib.get_cachedir()

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

Then I removed the matplotlib in my virtualenvironment:

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

Finally I removed the one in the .local folder:

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

Now importing matplotlib works fine. So when I run in 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

查看更多
来,给爷笑一个
3楼-- · 2019-01-14 15:50

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

pip2 uninstall backports.functools_lru_cache

then,

pip2 install backports.functools_lru_cache

fixed my problem.

查看更多
一夜七次
4楼-- · 2019-01-14 15:50

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

查看更多
爷的心禁止访问
5楼-- · 2019-01-14 15:51

I had the same problem and my solution was;

查看更多
Viruses.
6楼-- · 2019-01-14 15:56

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:

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.

查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-14 16:00

Also meet this issue on Ubuntu 16. Uninstall & reinstall not work for me.

My solution is reinstall from apt.

pip uninstall matplotlib
sudo apt-get autoremove python-matplotlib
sudo apt-get install python-matplotlib
查看更多
登录 后发表回答