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

Question:

I’m trying to run a django project on CentOS 7. I have a virtual environment inside my project containing all the required packages, … . I configured my httpd.conf file as follows:

<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:

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:

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

Asked By: user3080720

||

Answers:

There’s a little more to activating a virtualenv than just adding site-packages to the sys.path.

I’ve personally just executed the activate script right at the top of your wsgi module and it works well. 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. Note that I’m asserting the file exists, just because it’s easy to mess up paths.

Answered By: Ken Kinder

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. You also don’t need to be using site-packages.

See:

To work out what Python version mod_wsgi is compiled for, see:

Answered By: Graham Dumpleton

Run these commands:

$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi

$ sudo apt-get install libapache2-mod-wsgi-py3

Answered By: Meenakshi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.