views:

3217

answers:

3

I'm a complete noob here, so please bare with me on this one. Trying to get django to work with apache, and i'm getting the following error:

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

My django app is located in /home/user/django/MyDjangoApp/

My httpd.conf Location section looks like:

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

Please tell me how to correct the location section to make django work?

+9  A: 

I think mod_python is looking for settings in the MKSearch module which doesn't exist in side the /home/user/django/MyDjangoApp directory. Try adding the parent dir to the PythonPath directive as below:

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

Or remove the module name from the DJANGO_SETTINGS_MODULE env var as below:

<Location "/MyDjangoApp/">
  SetHandler python-program
  PythonHandler django.core.handlers.modpython
  SetEnv DJANGO_SETTINGS_MODULE settings
  PythonOption django.root /MyDjangoApp
  PythonPath "['/home/user/django/MyDjangoApp,'/var/www'] + sys.path" 
  PythonDebug On
</Location>
sleepyjames
+1 The django docs gloss over the difference between "/home/user/django" (which allows "from MyDjangoApp import foo") and alternatives.
Jarret Hardie
+4  A: 

Giving this answer for completeness - even though your case was different.

I've once named my django project test. Well, django was importing python module test - which is a module for regression testing and has nothing to do with my project.

This error will occur if python finds another module with the same name as your django project. Name your project in a distinct way, or prepend the path of the parent directory of your application to sys.path.

Evgeny
Exactly what was happening to me. Thanks!
nlaq
A: 

This works. Comments I've seen including from the Boston Python Meetup all concur mod_wsgi is easier to use. In my case on Red Hat EL 5 WS, changing from mod_python is not practical.

<Location />
        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE settings
        PythonOption django.root /home/amr/django/amr
        PythonPath "['/home/amr/django', '/home/amr/django/amr', '/usr/local/lib
/site-packages/django'] + sys.path"
        PythonDebug On
</Location>
octopusgrabbus