简体   繁体   中英

AttributeError: 'module' object has no attribute 'lru_cache'

Im getting the error as shown in title with this environment setup. Apache2 with mod_wsgi ,Python 3.5, Django 2.0.2 . I'm using virtualevn.

my virtual env is in : /home/santosh/Documents/project/project/ and django app is in /home/santosh/Documents/project/Reports

Below is the content of wsgi.py file

import os, sys


sys.path.append('/home/santosh/Documents/project/Reports/Reports')
sys.path.append('/home/santosh/Documents/project/Reports')


sys.path.append('/home/santosh/Documents/project/project/lib/python3.5/site-packages')
sys.path.append('/home/santosh/Documents/project/project/lib/python3.5')
sys.path.append('/home/santosh/Documents/project/project/bin')



from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Reports.settings")

application = get_wsgi_application()

Stacktrace:

[Sun Feb 04 20:40:39.396427 2018] [wsgi:error] [pid 6428:tid 140043928524544] [client 127.0.0.1:60276] mod_wsgi (pid=6428): Target WSGI script '/home/santosh/Documents/project/Reports/Reports/wsgi.py' cannot be loaded as Python module.

[Sun Feb 04 20:40:39.398284 2018] [wsgi:error] [pid 6428:tid 140043928524544] [client 127.0.0.1:60276] mod_wsgi (pid=6428): Exception occurred processing WSGI script '/home/santosh/Documents/project/Reports/Reports/wsgi.py'.

[Sun Feb 04 20:40:39.398425 2018] [wsgi:error] [pid 6428:tid 140043928524544] [client 127.0.0.1:60276] Traceback (most recent call last):

[Sun Feb 04 20:40:39.398475 2018] [wsgi:error] [pid 6428:tid 140043928524544] [client 127.0.0.1:60276] File "/home/santosh/Documents/project/Reports/Reports/wsgi.py", line 30, in

[Sun Feb 04 20:40:39.398555 2018] [wsgi:error] [pid 6428:tid 140043928524544] [client 127.0.0.1:60276] from django.core.wsgi import get_wsgi_application

[Sun Feb 04 20:40:39.398565 2018] [wsgi:error] [pid 6428:tid 140043928524544] [client 127.0.0.1:60276] File "/home/santosh/Documents/project/project/lib/python3.5/site-packages/django/ init .py", line 1, in

[Sun Feb 04 20:40:39.398591 2018] [wsgi:error] [pid 6428:tid 140043928524544] [client 127.0.0.1:60276] from django.utils.version import get_version

[Sun Feb 04 20:40:39.398598 2018] [wsgi:error] [pid 6428:tid 140043928524544] [client 127.0.0.1:60276] File "/home/santosh/Documents/project/project/lib/python3.5/site-packages/django/utils/version.py", line 61, in

[Sun Feb 04 20:40:39.398628 2018] [wsgi:error] [pid 6428:tid 140043928524544] [client 127.0.0.1:60276] @functools.lru_cache()

[Sun Feb 04 20:40:39.398643 2018] [wsgi:error] [pid 6428:tid 140043928524544] [client 127.0.0.1:60276] AttributeError: 'module' object has no attribute 'lru_cache'

I ran

sudo apt remove  libapache2-mod-wsgi
sudo apt install libapache2-mod-wsgi-py3

to install the Python3 version on Ubuntu 18.04 and afterwards reloaded Apache, which solved the error for me.

I stumbled here from Google looking for a result for CentOS 7, so for anyone in a similar position, here's what fixed it for me:

When you yum install mod_wsgi , it installs the python2 version. This causes pain and suffering and crazy errors like in OP.

The solution is to install the python36 (or whatever python3 version you're using) version from the IUS repo in CentOS. I had to download from pkgs.org because my system doesn't have external internet access, so I'm not sure how to do it from yum, but I can confirm that once I installed a compatible version of mod_wsgi everything started working.

After lots of trail and error and googling, finally i was able to run successfully.

I got the lru_cache error because, I did sudo apt-get install libapache2-mod-wsgi to install mod-wsgi. I think this might have installed to default python on Ubuntu 16.04 which is python2.7.

Solution: I uninstalled libapache2-mod-wsgi from ubuntu and installed it with pip install mod-wsgi and then finally copied the path of installed mod-wsgi from site-packages directory and updated it inside apache2/mods-available/wsgi.load file.

So its better to install mod-wsgi from virtualenv or normal python3 and then update apache files instead of installing libapache2-mod-wsgi directly on ubuntu.

How I solved it

I had django in a virtualenv and initially had installed uWSGI==2.0.19.1 system wide.

Following suggestions of other answers didn't solve my problem.

I then installed uWSGI==2.0.19.1 in my virtualenv, but the problem still happened.

Even the command which uwsgi pointed to my virtualenv:

/home/user/venv/bin/uwsgi

So I finally managed to make it work by calling uwsgi using the full path from my virtualenv:

/home/user/venv/bin/uwsgi --http :8001 --module my_django_project.wsgi

Then the problem has gone away.

I also see that when it was failing I had the message below:

detected binary path: /usr/local/bin/uwsgi

So I suppose just removing that /usr/local/bin/uwsgi would solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM