Django newbie deployment question - ImportError: Could not import settings 'settings'
Question: The app runs fine using django internal server however when I use apache +
mod_python I get the below error

* * *
    
    
      File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 75, in __init__
        raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
    
    ImportError: Could not import settings 'settings' (Is it on sys.path? Does it have syntax errors?): No module named settings
    

* * *

Here is the needed information

1) Project directory: /root/djangoprojects/mysite

2) directory listing of /root/djangoprojects/mysite

    
    
    ls -ltr
    total 28
    -rw-r--r-- 1 root root  546 Aug  1 08:34 manage.py
    -rw-r--r-- 1 root root    0 Aug  1 08:34 __init__.py
    -rw-r--r-- 1 root root  136 Aug  1 08:35 __init__.pyc
    -rw-r--r-- 1 root root 2773 Aug  1 08:39 settings.py
    -rw-r--r-- 1 root root 1660 Aug  1 08:53 settings.pyc
    drwxr-xr-x 2 root root 4096 Aug  1 09:04 polls
    -rw-r--r-- 1 root root  581 Aug  1 10:06 urls.py
    -rw-r--r-- 1 root root  314 Aug  1 10:07 urls.pyc
    

3) App directory : /root/djangoprojects/mysite/polls

4) directory listing of /root/djangoprojects/mysite/polls

    
    
    ls -ltr
    total 20
    -rw-r--r-- 1 root root 514 Aug  1 08:53 tests.py
    -rw-r--r-- 1 root root  57 Aug  1 08:53 models.py
    -rw-r--r-- 1 root root   0 Aug  1 08:53 __init__.py
    -rw-r--r-- 1 root root 128 Aug  1 09:02 views.py
    -rw-r--r-- 1 root root 375 Aug  1 09:04 views.pyc
    -rw-r--r-- 1 root root 132 Aug  1 09:04 __init__.pyc
    

5) Anywhere in the filesystem running import django in python interpreter
works fine

6) content of httpd.conf

    
    
    <Location "/mysite">
        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE settings
        PythonOption django.root /mysite
        PythonPath "['/root/djangoprojects/', '/root/djangoprojects/mysite','/root/djangoprojects/mysite/polls', '/var/www'] + sys.path"
        PythonDebug On
    </Location>
    

7) PYTHONPATH variable is set to

    
    
    echo $PYTHONPATH
    /root/djangoprojects/mysite
    

8) DJANGO_SETTINGS_MODULE is set to

    
    
    echo $DJANGO_SETTINGS_MODULE
    mysite.settings
    

9) content of sys.path is

    
    
    import sys
    >>> sys.path
    ['', '/root/djangoprojects/mysite', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/local/lib/python2.6/dist-packages']
    

How do I add settings location to sys.path such that it persistent across
sessions ?

I have read umpteen no of post with people having the same issue it and I have
tried a lot completely beats me as to what I need to do.

Looking for some help.

Thanks in advance Ankur Gupta


Answer: Your apache configuration should look like this:

    
    
    <Location "/mysite">
        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE mysite.settings
        PythonOption django.root /mysite
        PythonPath "['/root/djangoprojects/', '/root/djangoprojects/mysite','/root/djangoprojects/mysite/polls', '/var/www'] + sys.path"
        PythonDebug On
    </Location>
    

Note that the sole difference is the "mysite.settings". Don't forget to
restart apache once the config has changed (apache2ctl restart). See
<http://docs.djangoproject.com/en/dev/howto/deployment/modpython/> for more
info.

