python - Apache2 和 Django - [wsgi:error] ImportError: cannot import name 'get_version' -> No module named 'django'
问题描述
我已经在 Django 中建立了一个基本站点,并尝试使用 Apache 2 为其提供服务,但是在尝试时出现错误:
[Tue Jul 07 08:03:50.274206 2020] [mpm_event:notice] [pid 21614:tid 140492525521856] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Tue Jul 07 08:03:50.274292 2020] [core:notice] [pid 21614:tid 140492525521856] AH00094: Command line: '/usr/sbin/apache2'
[Tue Jul 07 08:03:55.841352 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] mod_wsgi (pid=21616): Target WSGI script '/home/ubuntu/Django/customspiritco/wsgi.py' cannot be loaded as Python module.
[Tue Jul 07 08:03:55.841401 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] mod_wsgi (pid=21616): Exception occurred processing WSGI script '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 08:03:55.841626 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] Traceback (most recent call last):
[Tue Jul 07 08:03:55.841655 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] File "/home/ubuntu/Django/customspiritco/wsgi.py", line 3, in <module>
[Tue Jul 07 08:03:55.841661 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] from django.core.wsgi import get_wsgi_application
[Tue Jul 07 08:03:55.841669 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] File "/home/ubuntu/Django/env/lib/python3.6/site-packages/django/__init__.py", line 1, in <module>
[Tue Jul 07 08:03:55.841745 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] from django.utils.version import get_version
[Tue Jul 07 08:03:55.841768 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] ImportError: cannot import name 'get_version'
我安装的版本是:Apache/2.4.29 (Ubuntu)、mod_wsgi/4.5.17、Python/3.6、Django/2.2.10
我的 djangoproject.conf 的 apache2 是:
<VirtualHost *:80>
ServerAdmin admin@customspirit.co
ServerName customspirit.co
DocumentRoot /home/ubuntu/Django
<Directory /home/ubuntu/Django>
Require all granted
</Directory>
Alias /static /home/ubuntu/Django/static_in_env
<Directory /home/ubuntu/Django/static_in_env>
Options FollowSymLinks
Order allow,deny
Allow from all
Require all granted
</Directory>
Alias /media /home/ubuntu/Django/media_root
<Directory /home/ubuntu/Django/media_root>
Options FollowSymLinks
Order allow,deny
Allow from all
Require all granted
</Directory>
# adjust the following line to match your Python path
WSGIDaemonProcess customspiritco python-home=/home/ubuntu/Django/env python-path=/home/ubuntu/Django
WSGIProcessGroup customspiritco
WSGIScriptAlias / /home/ubuntu/Django/customspiritco/wsgi.py
<Directory /home/ubuntu/Django/custompspiritco>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
错误所在的 init.py 文件如下所示:
from django.utils.version import get_version
VERSION = (2, 2, 10, 'final', 0)
__version__ = get_version(VERSION)
def setup(set_prefix=True):
"""
Configure the settings (this happens as a side effect of accessing the
first setting), configure logging and populate the app registry.
Set the thread-local urlresolvers script prefix if `set_prefix` is True.
"""
from django.apps import apps
from django.conf import settings
from django.urls import set_script_prefix
from django.utils.log import configure_logging
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
if set_prefix:
set_script_prefix(
'/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME
)
apps.populate(settings.INSTALLED_APPS)
wgsi.py 文件如下所示:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'customspiritco.settings.production')
application = get_wsgi_application()
每次我尝试加载任何内容时都会收到内部服务器错误(500),并且错误在日志中重复出现。
我现在已经将 Django 重新安装到 2.2.14 版本,它已经解决了这个问题并带来了一个新问题
[Tue Jul 07 09:03:28.291015 2020] [wsgi:error] [pid 25208:tid 139979472283392] [remote myip:myport] ModuleNotFoundError: No module named 'django'
在为我的 python 版本编译 mod_wgsi 后,此错误现在已替换为:
[Tue Jul 07 10:43:39.699154 2020] [wsgi:info] [pid 1305:tid 140703057624000] mod_wsgi (pid=1305): Adding '/home/ubuntu/Django' to path.
[Tue Jul 07 10:43:39.699699 2020] [mpm_event:debug] [pid 1305:tid 140702956263168] event.c(2218): AH02471: start_threads: Using epoll (wakeable)
[Tue Jul 07 10:44:08.714222 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of Require all granted: granted
[Tue Jul 07 10:44:08.714265 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of <RequireAny>: granted
[Tue Jul 07 10:44:08.714312 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of Require all granted: granted
[Tue Jul 07 10:44:08.714319 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of <RequireAny>: granted
[Tue Jul 07 10:44:08.727681 2020] [wsgi:info] [pid 1303:tid 140702905882368] mod_wsgi (pid=1303): Create interpreter 'customspirit.co|'.
[Tue Jul 07 10:44:08.740572 2020] [wsgi:info] [pid 1303:tid 140702905882368] mod_wsgi (pid=1303): Adding '/home/ubuntu/Django' to path.
[Tue Jul 07 10:44:08.740929 2020] [wsgi:info] [pid 1303:tid 140702905882368] [remote myip:myport] mod_wsgi (pid=1303, process='customspiritco', application='customspirit.co|'): Loading Python script file '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 10:44:08.741202 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] mod_wsgi (pid=1303): Failed to exec Python script file '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 10:44:08.741229 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] mod_wsgi (pid=1303): Exception occurred processing WSGI script '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 10:44:08.741396 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] Traceback (most recent call last):
[Tue Jul 07 10:44:08.741421 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] File "/home/ubuntu/Django/customspiritco/wsgi.py", line 3, in <module>
[Tue Jul 07 10:44:08.741427 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] from django.core.wsgi import get_wsgi_application
[Tue Jul 07 10:44:08.741441 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] ModuleNotFoundError: No module named 'django'
解决方案
经过数小时的挫折后,通过创建用户 ubuntu 拥有的新虚拟环境,一切都得到了解决。以前我一定是使用 sudo 完成的,这导致了找不到 Django 模块的问题
推荐阅读
- r - 在 RStudio 中使用 plm 中的 pgmm 编织时找不到对象
- r - 通过 write.csv 导出数据帧返回错误
- model-view-controller - 如何将参数从视图传递到控制器?
- node.js - Webpack/babel 意外的令牌,应为“;”
- firebase - Firebase Firestore 中 merge 和 mergeFields 的区别
- ios - 自定义字体在模拟器上工作,但在某些情节提要中没有呈现
- url - 如何通过 onTap 为网格布局中的每个网格触发 url?每个网格应该打开一个不同的 url
- c++ - 使用集合在 log(N) 中排序?
- fonts - 我怎么知道字体 URL 来自哪里?
- java - 我怎样才能把它变成一个 Java 正则表达式?