Wednesday, January 19, 2011

Django "Could not import settings 'settings.py'" error.

I've already done my best to follow the instructions at http://docs.djangoproject.com/en/dev/howto/deployment/modpython/, but a customer is transferring a website to us, and I suspect the original developer's methods were a bit, uh, different.

So, first the full error message:

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

Then, the apache configuration for the site:

<Location /acecoach/>
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE settings.py
    PythonOption django.root /acecoach
    PythonPath "['/home/acecoach/public_html/acecoach'] + sys.path"
    PythonDebug On
</Location>

Now, the "settings module" as far as I know, is located in /home/acecoach/public_html/acecoach/settings.py This file is readable by the apache server - I tested this by actually SU-ing to the apache user and reading the file from the command line.

I've also read similar advice on this error message, and found no useful help in this regard. It's driving me nuts. :)

  • Hey , i had same problem in mod_python too , but when i migrate to apache + mod_wsgi all of my proplems solved .
    why you didn't try mod_wsgi ?
    it's newer than mod_python and haven't such problems .
    but if wanna solve it , you can go to this address :
    http://stackoverflow.com/questions/1216340/django-newbie-deployment-question-importerror-could-not-import-settings-setti

    Graham Dumpleton : Yours was a different issue. This person was using Python file extension when they shouldn't have.
    From Ansari
  • Remove the .py file extension and add the project context to your settings module definition. Assuming that your project is called acecoach.

    SetEnv DJANGO_SETTINGS_MODULE acecoach.settings
    

    The Python documentation explains the reason simpler than I shall attempt to.

    http://docs.python.org/tutorial/modules.html#modules

    A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

    http://docs.python.org/tutorial/modules.html#packages

    Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A.

    Graham Dumpleton : Because they only have directory '/home/acecoach/public_html/acecoach' in PythonPath, they would actually use 'settings' and not 'acecoach.settings'. Rather than do that though, they should add '/home/acecoach/public_html' to PythonPath as well and keep using 'acecoach.settings'.
    From Dan Carley

0 comments:

Post a Comment