Archive

Django Settings Import Error Apache Mod_Python Fix

When moving a Django project from one server to another you may run into a settings import error like this one:

raise Import
Error, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)

 ....

First, make sure that your file permissions are setup correctly.  If the apache web server can't at least read Django projects files you will receive a mod_python import error.  Also make sure that your project name isn't the same as a module that you might be importing.

 Here is an example httpd.conf virtual host settings:

<VirtualHost *:80>
    ServerAdmin [contact form]

    ServerName test.com
    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/>
        AddHandler cgi-script .cgi
        Options Indexes FollowSymLinks MultiViews +ExecCGI
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
#note that you may have to change your location to where you have your project setup

#i.e.<Location "/myproject"> as well as your settings  - i.e. myproject.settings

   <Location "/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE settings
    PythonDebug On
    PythonPath "['/djangohome/'] + sys.path"
    </Location>

    <Location "/media/">
    SetHandler None
    </Location>
    <Location "/adminmedia/">
    SetHandler None
    </Location>
</VirtualHost>

Still can't solve -Click Here to Ask the Geek!