functools has no attribute lru_cache - python

I'm using Python 3.7 on my Windows
This error occurs while running every code:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import nltk
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\nltk\__init__.py", line 99, in <module>
from nltk.internals import config_java
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\nltk\internals.py", line 11, in <module>
import subprocess
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 50, in <module>
import signal
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\signal.py", line 3, in <module>
from functools import wraps as _wraps
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\functools.py", line 21, in <module>
from collections import namedtuple
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\collections\__init__.py", line 22, in <module>
from keyword import iskeyword as _iskeyword
File "C:\Users\HP\Desktop\tweepy\keyword.py", line 1, in <module>
import re
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\re.py", line 297, in <module>
#functools.lru_cache(_MAXCACHE)
AttributeError: module 'functools' has no attribute 'lru_cache'
How to fix it?

It looks like someone published a functools package on pypi, so if you had run:
# don't run this!
pip install functools
You may have accidentally installed that package. If you encounter this error, I would:
pip uninstall functools
To make sure that the functools you are using are the base package functools.
(I ended up here because I attempted to use functools.cache, which seems to only be present in Python 3.8, not earlier.)

I had the same error recently and it was because I had a file called functools.py in my project. Renaming it fixed the issue.

Related

collection.Mutablemapping Error in pyrebase Import

I have installed pyrebase library and tried importing it Like
import pyrebase
It gives following Error and i dont know how to fix it!
Traceback (most recent call last):
File "fire.py", line 1, in <module>
import pyrebase
File "env\lib\site-packages\pyrebase\__init__.py", line 1, in <module>
from .pyrebase import initialize_app
File "env\lib\site-packages\pyrebase\pyrebase.py", line 1, in <module>
import requests
File "env\lib\site-packages\requests\__init__.py", line 63, in <module>
from . import utils
File "env\lib\site-packages\requests\utils.py", line 27, in <module>
from .cookies import RequestsCookieJar, cookiejar_from_dict
File "env\lib\site-packages\requests\cookies.py", line 172, in <module>
class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
AttributeError: module 'collections' has no attribute 'MutableMapping'
Tried following some stack overflow advice but those wewe not specific.
This happens because pyrebase uses a deprecated collections module. Fixing this issue is possible but it leads to a chain of issues, I would recommend you to used pyrebase4 instead.

ImportError: cannot import name 'binary_type'

I've installed BSON library to convert strings to MongoDB ObjectIds, but when I try using the library, this comes up:
Traceback (most recent call last):
File "search.py", line 7, in <module>
from pymongo.mongo_client import MongoClient
File "/home/user/anaconda3/lib/python3.6/site-packages/pymongo/__init__.py", line 90, in <module>
from pymongo.common import (MIN_SUPPORTED_WIRE_VERSION,
File "/home/user/anaconda3/lib/python3.6/site-packages/pymongo/common.py", line 21, in <module>
from pymongo.auth import MECHANISMS
File "/home/user/anaconda3/lib/python3.6/site-packages/pymongo/auth.py", line 33, in <module>
from bson.binary import Binary
File "/home/user/anaconda3/lib/python3.6/site-packages/bson/binary.py", line 21, in <module>
from bson.py3compat import PY3, binary_type
ImportError: cannot import name 'binary_type'
Also pip installs py3compat, but there's no 'binary_type' in there.
Any idea how to fix this?
I saw this error and got it fixed by installing the newest version of pymongo.
(at the time of writing)
pip install pymongo==3.5.1

Can't import module ANTLR MyGrammarLexer and MyGrammarParser

I'm trying to start with ANTLR . When I import module antlr it's working just fine , but if I try to import MyGrammarLexer and MyGrammarParser , it's shows that MyGrammarLexer and Parser aren't in lib. I Using PyCharm , I installed ANTLR with : pip3 install antlr4-python3-runtime my code is :
import sys
from antlr4 import *
import MyGrammarLexer
import MyGrammarParser
def main(argv):
input = FileStream(argv[1])
lexer = MyGrammarLexer(input)
stream = CommonTokenStream(lexer)
parser = MyGrammarParser(stream)
tree = parser.startRule()
if __name__ == '__main__':
main(sys.argv)
Maybe whom know why i can't import MyGrammarLexer and MyGrammarParser?
Please suggest!
Tackback:
/usr/bin/python3.6 /home/andrejka/PycharmProjects/Parser/parser.py
Traceback (most recent call last):
File "/home/andrejka/PycharmProjects/Parser/parser.py", line 2, in <module>
from antlr4 import *
File "/usr/lib/python3/dist-packages/antlr4/__init__.py", line 5, in <module>
from antlr4.BufferedTokenStream import TokenStream
File "/usr/lib/python3/dist-packages/antlr4/BufferedTokenStream.py", line 19, in <module>
from antlr4.error.Errors import IllegalStateException
File "/usr/lib/python3/dist-packages/antlr4/error/Errors.py", line 5, in <module>
from antlr4.atn.Transition import PredicateTransition
File "/usr/lib/python3/dist-packages/antlr4/atn/Transition.py", line 19, in <module>
from __builtin__ import unicode
ModuleNotFoundError: No module named '__builtin__'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "/home/andrejka/PycharmProjects/Parser/parser.py", line 2, in <module>
from antlr4 import *
File "/usr/lib/python3/dist-packages/antlr4/__init__.py", line 5, in <module>
from antlr4.BufferedTokenStream import TokenStream
File "/usr/lib/python3/dist-packages/antlr4/BufferedTokenStream.py", line 19, in <module>
from antlr4.error.Errors import IllegalStateException
File "/usr/lib/python3/dist-packages/antlr4/error/Errors.py", line 5, in <module>
from antlr4.atn.Transition import PredicateTransition
File "/usr/lib/python3/dist-packages/antlr4/atn/Transition.py", line 19, in <module>
from __builtin__ import unicode
ModuleNotFoundError: No module named '__builtin__'
I ran into this problem recently when getting the same error using the pddlpy python library. I was able to fix it by doing the following:
pip3 install antlr4-python3-runtime==4.9
The version 4.9 at the end is very important.
I found out that content of the package:
https://pypi.python.org/packages/0b/6b/30c5b84d203b62e1412d14622e3bae6273399d79d20f3a24c8145213f610/antlr4-python3-runtime-4.7.tar.gz#md5=190245a0fb4abf43568489a4b6e33aba
is different from the package installed by pip3.
I have replaced content of:
~/anaconda3/envs/py3/lib/python3.6/site-packages/antlr4
with content from the package I have downloaded and extracted manually:
~/Downloads/antlr4-python3-runtime-4.7/src/antlr4
It seems to be working now - in particular the following error:
ModuleNotFoundError: No module named '__builtin__'
does not occur.
After posting your traceback I can see the error is:
ModuleNotFoundError: No module named '__builtin__'
That means the package you try to import is using python2 syntax, but you're using python3.
You should use python2 if you need to run this package, but keep in mind that you probably want to search for a different one if this is a new project.
More about why it's not working here
EDIT
They released a package with python3 support, but it seems you installed/used something different. You should verify it.

Random errors in Anaconda3

C:\Users\jesper>pip Traceback (most recent call last): File
"D:\Anaconda3\Scripts\pip-script.py", line 3, in <module>
import pip File "D:\Anaconda3\lib\site-packages\pip\__init__.py", line 14, in <module>
from pip.utils import get_installed_distributions, get_prog File "D:\Anaconda3\lib\site-packages\pip\utils\__init__.py", line 22, in
<module>
from pip.compat import console_to_str, expanduser, stdlib_pkgs File "D:\Anaconda3\lib\site-packages\pip\compat\__init__.py", line 11,
in <module>
from logging.config import dictConfig as logging_dictConfig File "D:\Anaconda3\lib\logging\config.py", line 30, in <module>
import logging.handlers File "D:\Anaconda3\lib\logging\handlers.py", line 26, in <module>
import logging, socket, os, pickle, struct, time, re File "D:\Anaconda3\lib\socket.py", line 123, in <module>
class socket(_socket.socket): TypeError: __weakref__ slot disallowed: either we already got one, or __itemsize__ != 0
I get this now, and it happened randomly and keeps recurring. I can't use Python at the moment. I tried re-installing the entire conda library and tried different locations for Anaconda. Even tried installing Anaconda for 2.7 and it still didn't work. If someone knows how to fix I'd be in there eternal debt. Thanks!

Python 2.7.3 import functools gives me an AttributeError: 'module' object has no attribute 'compose' [duplicate]

I installed Ubuntu 12.04 64 bit on a new system, and cannot install functools. I have installed this multiple times but do not remember getting this error, and cannot find any solution through Google. What do I need to do?
(myvenv)bobs#myvenv:~$ pip install functools
Downloading/unpacking functools
Downloading functools-0.5.tar.gz
Running setup.py egg_info for package functools
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/home/bobs/.virtualenvs/myvenv/local/lib/python2.7/site-packages/setuptools/__init__.py", line 2, in <module>
from setuptools.extension import Extension, Library
File "/home/bobs/.virtualenvs/myvenv/local/lib/python2.7/site-packages/setuptools/extension.py", line 5, in <module>
from setuptools.dist import _get_unpatched
File "/home/bobs/.virtualenvs/myvenv/local/lib/python2.7/site-packages/setuptools/dist.py", line 10, in <module>
from setuptools.compat import numeric_types, basestring
File "/home/bobs/.virtualenvs/myvenv/local/lib/python2.7/site-packages/setuptools/compat.py", line 17, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 71, in <module>
import socket
File "/usr/lib/python2.7/socket.py", line 49, in <module>
from functools import partial
File "functools.py", line 72, in <module>
globals()['c_%s' % x] = globals()[x] = getattr(_functools, x)
AttributeError: 'module' object has no attribute 'compose'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "/home/bobs/.virtualenvs/myvenv/local/lib/python2.7/site-packages/setuptools/__init__.py", line 2, in <module>
from setuptools.extension import Extension, Library
File "/home/bobs/.virtualenvs/myvenv/local/lib/python2.7/site-packages/setuptools/extension.py", line 5, in <module>
from setuptools.dist import _get_unpatched
File "/home/bobs/.virtualenvs/myvenv/local/lib/python2.7/site-packages/setuptools/dist.py", line 10, in <module>
from setuptools.compat import numeric_types, basestring
File "/home/bobs/.virtualenvs/myvenv/local/lib/python2.7/site-packages/setuptools/compat.py", line 17, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 71, in <module>
import socket
File "/usr/lib/python2.7/socket.py", line 49, in <module>
from functools import partial
File "functools.py", line 72, in <module>
globals()['c_%s' % x] = globals()[x] = getattr(_functools, x)
AttributeError: 'module' object has no attribute 'compose'
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /home/bobs/.virtualenvs/myvenv/build/functools
Storing complete log in /home/bobs/.pip/pip.log
Python2.7 comes with the functools module included.
You can install functools32 if you want to get the lru-cache decorator, which was introduced with Python3.2.
Edit: I actually checked this. I got the same error when I tried to pip-install functools with Python2.7. Simply do import functools and proceed as usual.
Make sure that it is functools32 in python version 2.x. In 3.x the tools come inbuilt.
Got a solution on windows.
Delete anything reference to functools in site-packages folder.
easy_install -U pip==7.1.2

Categories

Resources