views:

3246

answers:

2

Hello folks,

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

+2  A: 

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.

Gerald Senarclens de Grancy
made the change you mentioned so now it appears as <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>for restart I did the following /etc/init.d/apache2 restartstill no luck./etc/init.d/apache2 restart
Ankur Gupta
made the change of settings to mysite.settings still the same error.
Ankur Gupta
maybe the long PythonPath confuses mod_python. Try just having PythonPath "['/root/djangoprojects/'] + sys.path"Also, make sure that Apache has the right to read the settings file (read permission for others) and the whole path until the file (read and execute permission for others on /root, /root/djangoprojects and /root/djangoprojects/mysite). Though it would be better to move the whole project to a location outside /root, after all it shouldn't be there.
Gerald Senarclens de Grancy
Thanks Gerald ... Having the app in /root was leading to some unknow issues ... took the app in /var/www and it works. I made one more change i enforced PYTHONPATH for all systems users including www-data. Thanks man.
Ankur Gupta
Be careful when putting the app in /var/www - I would consider this a security problem and not do so. Rather put it in something like /usr/local/django-apps or somewhere in a designated user's home.
Gerald Senarclens de Grancy
and I thought I was done :( ... sure I will change it soon.
Ankur Gupta
What is the security problem of putting an app in /var/www/?
hughdbrown
Go to http://docs.djangoproject.com/en/dev/intro/tutorial01/#intro-tutorial01 and look for "Where should this code live?"
Gerald Senarclens de Grancy
+1  A: 

Try changing to the following:

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

Use no "/" at the end of the PythonPath entries (that may be an issue, I had problems with that at least on Windows).

The entries '/root/djangoprojects/mysite','/root/djangoprojects/mysite/polls' are not needed since you will be referring to your modules by full name (i.e. mysite.sttings means the settings module inside the mysite package, which is the correct way to access it in the PythonPath /root/djangoprojects).

Carles Barrobés