Django with apache and mod_wsgi issues

by Stii

I ran into a fairly common error while setting up Django with mod_wsgi and Apache on Debian. What irritates me about it is that I didn’t pick it up right away, but I’ll blame the long Easter weekend for that. :P

django-logo-positive

ImportError: Could not import settings 'mysite.settings'
(Is it on sys.path? Does it have syntax errors?):
No module named mysite.settings

My wsgi file looked like this:

import os, sys
sys.path.append('/home/djangoprojects/mysite/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi

_application = django.core.handlers.wsgi.WSGIHandler()

def application(environ, start_response):
    if environ['wsgi.url_scheme'] == 'https':
        environ['HTTPS'] = 'on'
    return _application(environ, start_response)

Like an idiot, I was looking everywhere for the issue, accept the glaring obvious reason. I checked permissions, created a .pth file for the project in the /site-packages/ directory in the Python library. Trust me, I did everything, but the obvious thing.

See, here is that obvious reason:

I append the directory “/home/djangoprojects/mysite” to my sys.path which is wrong. Python then looks for the module “mysite” in the directory /home/djangoprojects/mysite which in fact doesn’t exist. Obvious, no?

To fix this problem, you need to append the directory “/home/djangoprojects/” to your sys.path and it will find the module mysite.settings no problem and it will run smooth.

Long weekend, surf on the brain. That’s what is wrong!