"ImportError: No module named django.core.wsgi" apache error

"ImportError: No module named django.core.wsgi" apache error

我正在尝试 运行 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 重新启动正常,但是当我访问该网站时,返回 "Internal Server error" 并且 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

卡在这个错误两天了,不知道怎么办

激活 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 中完成 运行 所需的完整路径操作。请注意,我断言该文件存在,只是因为它很容易弄乱路径。

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

参见:

要了解 Python 版本 mod_wsgi 的编译目的,请参阅: