Mod_wsgi fails to load django.core.handlers.wsgi
Question: Ok, after 5-6 hours of trying, I give up. I have searched the web, tried all
solutions suggested, but nothing is solving my problem.

**Goal:** Set up Django on my Ubuntu 12.04 VPS.

**Problem:** `Exception occurred processing WSGI script [...] ImportError: No
module named django.core.handlers.wsgi` in /etc/log/apache2/error.log.

**Solutions tried:** 1) Appending the site-packages directory to the sys path
in Djangos' wsgi.py file, 2) re-installing mod_wsgi, 3) making sure mod_wsgi
is compiled for the same Python version as Django is installed with, 4) chmod
777 for the site-packages directory.

**Environment:** Ubuntu 12.04 VPS, Django installation in virtualenv, Python
version 2.7.3, Django version 1.6.1, mod_wsgi built from
[mod_wsgi-3.4.tar.gz](https://code.google.com/p/modwsgi/downloads/list).

**Full error message:**

    
    
    mod_wsgi (pid=23691): Exception occurred processing WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py'.
    Traceback (most recent call last):
       File "/var/www/mySite/djangoSite/djangoSite/wsgi.py", line 7, in <module>
         import django.core.handlers.wsgi
    ImportError: No module named django.core.handlers.wsgi
    mod_wsgi (pid=23691): Target WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py' cannot be loaded as Python module.
    
    mod_wsgi (pid=23691): Exception occurred processing WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py'.
    Traceback (most recent call last):
       File "/var/www/mySite/djangoSite/djangoSite/wsgi.py", line 7, in <module>
         import django.core.handlers.wsgi
    ImportError: No module named django.core.handlers.wsgi
    

**Conf file from sites-available:**

    
    
    <VirtualHost *:80>
        ServerAdmin admin@mysite.com
        ServerName mysite.com
    
        DocumentRoot /var/www/mysite.com/djangoSite
    
        WSGIDaemonProcess djangoSite python-path=/var/www/mysite.com/djangoSite:~/Envs/myEnv/lib/python2.7/site-packages
        WSGIProcessGroup djangoSite
        WSGIScriptAlias / /var/www/mysite.com/djangoSite/djangoSite/wsgi.py
    
        Alias /static/ /var/www/mysite.com/djangoSite/static/
    
        <Directory /var/www/mysite.com/djangoSite/static>
            Order deny,allow
            Allow from all
        </Directory>
    
        <Directory /var/www/mysite.com/djangoSite/djangoSite>
            <Files wsgi.py>
                Order deny,allow
                Allow from all
            </Files>
        </Directory>
    </VirtualHost>
    

**wsgi.py:**

    
    
    import os
    import sys
    
    sys.path.append('/var/www/mysite.com/djangoSite/')
    sys.path.append('/var/www/mysite.com/djangoSite/djangoSite/')
    
    activate_this = '/root/Envs/myenv/bin/activate_this.py'
    execfile(activate_this, dict(__file__=activate_this))
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoSite.settings'
    
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    

**Site structure**

    
    
    /var/www/
    -- mysite.com
      -- djangoSite
        -- manage.py
        -- djangoSite
          -- settings.py, wsgi.py etc.
    


Answer: In your wsgi.py, try adding this to activate virtual env:

    
    
    import os
    import sys
    
    sys.path.append('/Path_To/Virtual_Env/Project_Dir/')
    
    #This is important if multiple apps are running (instead of setdefault)
    os.environ["DJANGO_SETTINGS_MODULE"] = "app_name.settings"
    
    activate_this = '/Path_To/Virtual_Env/bin/activate_this.py'
    execfile(activate_this, dict(__file__=activate_this))
    

In you apache config, you mainly need only(No Deamon Process required):

    
    
    WSGIScriptAlias / /var/www/mysite.com/djangoSite/djangoSite/wsgi.py
    Alias /static/ /var/www/mysite.com/djangoSite/static/
    

See from the **Create Virtual Host** section of [this
link](http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-
mod_wsgi/)

