[英]“ImportError: No module named django.core.wsgi” apache error
I'm trying to run a django project on CentOS 7. I have a virtual environment inside my project containing all the required packages, ... . 我正在尝试在CentOS 7上运行django项目。我的项目中有一个虚拟环境,其中包含所有必需的软件包,...。 I configured my httpd.conf file as follows:
我将httpd.conf文件配置如下:
<VirtualHost *:80>
ServerName the_server_ip_address
ServerAlias localhost
DocumentRoot /var/www/html
# adding these lines for handling static files
Alias /media/ /var/www/html/wsgi-scripts/walk/mysite/static/media
Alias /static/ /var/www/html/wsgi-scripts/walk/mysite/static/static_root/
<Directory /var/www/html>
#Order allow,deny
#Allow from all
Require all granted
Satisfy Any
</Directory>
WSGIDaemonProcess localhost processes=2 threads=15 display-name=%{GROUP} python-home=/var/www/html/wsgi-scripts/walk/walk.venv python-path=/var/www/html/wsgi-scripts/walk/mysite
WSGIProcessGroup localhost
WSGIScriptAlias / /var/www/html/wsgi-scripts/walk/mysite/mysite/wsgi.py
<Directory /var/www/html/wsgi-scripts/walk/mysite/mysite>
WSGIPassAuthorization On
<Files wsgi.py>
Require all granted
</Files>
#Require all granted
</Directory>
</VirtualHost>
and my wsgi.py is configured as follows: 我的wsgi.py的配置如下:
import os, sys
# add the mysite project path into the sys.path
sys.path.append('/var/www/html/wsgi-scripts/walk/mysite')
# add the virtualenv site-packages path to the sys.path
sys.path.append('/var/www/html/wsgi-
scripts/walk/walk.venv/lib/python2.7/site-packages')
# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
The httpd is restarting fine, but when I access the website, an "Internal Server error" is returned and there are the following lines inside the apache log: httpd重新启动正常,但是当我访问网站时,返回“内部服务器错误”,并且apache日志中包含以下几行:
(most recent call last):
File "/var/www/html/wsgi-scripts/walk/mysite/mysite/wsgi.py", line 21, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
mod_wsgi (pid=11196): Target WSGI script '/var/www/html/wsgi-scripts/walk/mysite/mysite/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=11196): Exception occurred processing WSGI script '/var/www/html/wsgi-scripts/walk/mysite/mysite/wsgi.py'.
Traceback (most recent call last):
File "/var/www/html/wsgi-scripts/walk/mysite/mysite/wsgi.py", line 21, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
I have stucked in this error for two days and I don't know what to do. 我在此错误中停留了两天,我不知道该怎么办。
There's a little more to activating a virtualenv than just adding site-packages to the sys.path. 激活virtualenv不仅仅是将站点程序包添加到sys.path。
I've personally just executed the activate script right at the top of your wsgi module and it works well. 我个人刚刚在wsgi模块顶部执行了激活脚本,它运行良好。 Eg,
例如,
# Run find . -iname 'activate_this.py' and just paste the path here.
env_file="/var/www/html/wsgi-scripts/walk/walk.venv/bin/activate_this.py"
assert os.path.exists(env_file)
# Run the activation
execfile(env_file, dict(__file__=env_file))
That'll do the full path manipulations necessary to run in the virtualenv. 这将完成在virtualenv中运行所需的完整路径操作。 Note that I'm asserting the file exists, just because it's easy to mess up paths.
请注意,我断言该文件存在,只是因为它很容易弄乱路径。
If /var/www/html/wsgi-scripts/walk/walk.venv
is definitely the same as sys.prefix
for your virtual environment where Django is installed, then the issue is likely that mod_wsgi is compiled for a different version of Python that your virtual environment was created from. 如果
/var/www/html/wsgi-scripts/walk/walk.venv
与安装Django的虚拟环境的sys.prefix
绝对相同,则问题可能是mod_wsgi是为不同版本的Python编译的您的虚拟环境是从中创建的。 You also don't need to be using site-packages
. 您也不需要使用
site-packages
。
See: 看到:
To work out what Python version mod_wsgi is compiled for, see: 要弄清楚编译的Python版本mod_wsgi是什么,请参阅:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.