python 2.7 functools_lru_cache does not import although installed
Asked Answered
M

14

35

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

Mares answered 8/11, 2017 at 12:12 Comment(0)
P
40

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.

Pierrette answered 17/3, 2018 at 12:23 Comment(5)
I had the same problem after updating Ubuntu to 18.04. This answer solved it for me. Note: I also had to uninstall matplotlib before via pip: pip uninstall matplotlibApyretic
There should not be any reason for downgrading matplotlib. If someone thinks there is an issue with matplotlib, better inform the developpers about it. However, seen from the other answers here this problem should be solvable by getting the environment into which matplotlib is installed correctly set up.Toluca
This works like a charm. It also solves the mysterious import error of 'cbook' ( #44623876 ).Decimeter
This worked! thanks a lot! I am trying to figure out the problem for more than an hour. Reinstalling my python, messing with the files on the lib/site-packages folder.Cynical
github.com/matplotlib/matplotlib/issues/…Macaluso
S
30

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.

Solubilize answered 10/11, 2017 at 3:9 Comment(7)
I'm experiencing the same issue. The uninstall/installation of backports.functools_lru_cache did not help. I am using Ubuntu 16.04, Python 27, pip 9.0.1, too)Refreshment
I'm using Ubuntu 14.04, Python 2.7.14 and pip 9.0.1. It worked for me.Solubilize
@Refreshment were you able to solve the issue, I am facing the same.Immunity
@saharudra Yes, for my situation. See my post below.Refreshment
@Refreshment I did try that but of no use. I uninstalled matplotlib, even uninstalled python2.7 and started from scratch but of no use.Immunity
Did you try installing using anaconda?Solubilize
for Python2.7 it does not help, try from backports.functools_lru_cache import lru_cacheMacaluso
W
20

Install arrow using:

pip install arrow==0.12.0 

fixed this issue for me

Winfrid answered 24/8, 2018 at 14:30 Comment(2)
Interesting... this just worked for me. It resulted in uninstalling backports.functools-lru-cache-1.5 and then installing backports.functools-lru-cache-1.2.1. What's not obvious to me is why this fixes anything.. in an interactive session the import worked fine with version 1.5.Intrastate
Woo, magic! that works for me, with Python 2.7.16 and resulted in downgrade to backports.functools_lru_cache==1.2.1Leghorn
R
14

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.

Refreshment answered 15/11, 2017 at 21:38 Comment(1)
This worked for me, perhaps pip being pip3 is a new thingSunbow
J
9

You have to check what is the import path of backports package:

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

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

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)

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

Use pip2 command for python2.

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

Jactitation answered 30/6, 2018 at 18:2 Comment(1)
The ''--user'' is exactly what is needed in a virtualenvDurkee
T
7

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

which will avoid installing arrow merely to downgrade functools_lru_cache.

Tankoos answered 2/12, 2019 at 3:38 Comment(0)
G
3

I had the same problem and my solution was;

Gaming answered 5/12, 2017 at 14:48 Comment(0)
S
2

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

Steve answered 10/1, 2018 at 12:20 Comment(0)
V
2

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

  • pip2 uninstall matplotlib
  • sudo apt-get autoremove python-matplotlib
  • sudo apt-get install python-matplotlib
Verdieverdigris answered 2/2, 2019 at 9:23 Comment(0)
G
2

If you're experiencing this issue on Ubuntu 18.x (and maybe other versions): it is caused by this Ubuntu bug. The problem is in the Ubuntu package python-configparser which is installed as a dependence of python-pip. It contains empty __init__ file

/usr/lib/python2.7/dist-packages/backports/__init__.py

Hence, pip modules installed to /usr/local/lib/python2.7/dist-packages/backports are simply ignored. As soon as this bug is not fixed yet, the easiest way to fix this is to remove this package:

sudo apt remove python-configparser

It does not any harm to any of your backports modules installed by pip (since they are in another folder), and this is much safer than version downgrade.

See also this pip issue with similar problem around backports module.

Goebel answered 23/5, 2020 at 19:32 Comment(2)
This solved the same problem on my raspberry pi.Hhd
It solved me the problem on Raspberry Pi too, while I was trying to play with BeautifulSoup4 using CSS selectors.Nl
E
1

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

Elisha answered 12/7, 2018 at 9:33 Comment(1)
Please try adding an explanation.Cutty
G
1

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
Galoot answered 21/10, 2018 at 16:26 Comment(0)
E
1

This worked for me. Ubuntu 18.04.

sudo pip2 uninstall backports.functools-lru-cache
sudo apt install python-backports.functools-lru-cache
Einhorn answered 13/3, 2020 at 6:9 Comment(0)
W
0

this worked for me

from backports.functools_lru_cache import lru_cache
Weatherproof answered 24/6, 2018 at 6:18 Comment(1)
This is giving following error on Ubuntu 18: '''ImportError: No module named functools_lru_cache'''Tourbillion

© 2022 - 2024 — McMap. All rights reserved.