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

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

我在使用此环境设置时遇到标题中所示的错误。 Apache2 与 mod_wsgi ,Python 3.5,Django 2.0.2。我正在使用 virtualevn。

我的虚拟环境在:/home/santosh/Documents/project/project/ django 应用程序在 /home/santosh/Documents/project/Reports

下面是wsgi.py文件的内容

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()

堆栈跟踪:

[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'

经过大量的跟踪、错误和谷歌搜索,我终于能够 运行 成功。

我收到 lru_cache 错误,因为我执行了 sudo apt-get install libapache2-mod-wsgi 安装 mod-wsgi。我认为这可能已安装到 Ubuntu 16.04 上的默认 python,即 python2.7。

解决方案:我从ubuntu卸载了libapache2-mod-wsgi,然后用pip install mod-wsgi安装,最后从站点复制安装的mod-wsgi路径-packages 目录并在 apache2/mods-available/wsgi.load 文件中更新它。

所以最好从 virtualenv 或正常安装 mod-wsgi python3 然后更新 apache 文件而不是直接在 [=31= 上安装 libapache2-mod-wsgi ].

我从 Google 在这里偶然发现了 CentOS 7 的结果,所以对于处于类似位置的任何人,以下是为我修复它的方法:

当您 yum install mod_wsgi 时,它会安装 python2 版本。这会导致像 OP 中那样的痛苦和痛苦以及疯狂的错误。

解决方案是从 CentOS 中的 IUS 存储库安装 python36(或您正在使用的任何 python3 版本)版本。我不得不从 pkgs.org 下载,因为我的系统没有外部互联网访问权限,所以我不确定如何从 yum 下载,但我可以确认,一旦我安装了 [=18= 的兼容版本] 一切开始工作。

我运行

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

在 Ubuntu 18.04 上安装 Python3 版本,然后重新加载 Apache,这为我解决了错误。

我是怎么解决的

我在 virtualenv 中安装了 django,并且最初安装了 uWSGI==2.0.19.1 系统范围。

遵循其他答案的建议并没有解决我的问题。

然后我在我的virtualenv中安装了uWSGI==2.0.19.1,但问题仍然存在。

连命令which uwsgi都指向我的virtualenv:

/home/user/venv/bin/uwsgi

所以我终于设法通过使用来自我的 virtualenv 的完整路径调用 uwsgi 使其工作:

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

那么问题就解决了。

我还看到,当它失败时,我收到了以下消息:

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

所以我想只要删除 /usr/local/bin/uwsgi 就可以解决问题。