“ImportError:没有名为django.core.wsgi的模块”apache错误

时间:2018-06-08 18:03:08

标签: python django apache mod-wsgi

我正在尝试在CentOS 7上运行一个django项目。我的项目中有一个虚拟环境,包含所有必需的包,....我按如下方式配置了我的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>

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

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

我已经在这个错误中坚持了两天而且我不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

激活virtualenv不仅仅是将站点包添加到sys.path。

我个人刚刚在wsgi模块的顶部执行了激活脚本,效果很好。例如,

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

那将完成在virtualenv中运行所需的完整路径操作。请注意,我断言文件存在,只是因为它很容易弄乱路径。

答案 1 :(得分:0)

如果/var/www/html/wsgi-scripts/walk/walk.venv与安装Django的虚拟环境的sys.prefix绝对相同,则问题可能是mod_wsgi是针对从中创建虚拟环境的不同版本的Python编译的。您也不需要使用site-packages

请参阅:

要弄清楚Python版本mod_wsgi的编译内容,请参阅: