Django manage.py syncdb can't find my settings file on CentOS

2.8k Views Asked by At

I'm deploying my django application onto a CentOS 5.5 server, with django-1.1.4 over python-2.6.5.

I have multiple settings files inside the myapp/settings/ folder.

I would like to run the syncdb; here's what I do (with myapp inside myproject folder):

$> cd /var/www/apps/myproject
$> export PYTHONPATH=/var/www/apps/myproject
$> export DJANGO_SETTINGS_MODULE=myapp.settings.my_serverconfig
$> python26 myapp/manage.py syncdb

Django then issues an error like this :

Error: Can't find the file 'settings.py' in the directory containing 'myapp/manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)

Traceback (most recent call last):
  File "emon/manage.py", line 17, in <module>
    execute_manager(settings)
  File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 360, in execute_manager
    setup_environ(settings_mod)
File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 343, in setup_environ
    project_module = import_module(project_name)
File "/usr/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named my_serverconfig

In the myapp.wsgi file, os.path is appended with myproject path, and the os.environ['DJANGO_SETTINGS_MODULE'] is set also. Apache (through mod_wsgi) can start the app with no such error.

Finally, this works under Windows, where I run python-2.6.6 with django-1.1.1.

$> d:
$> cd d:\Code\myproject
$> export PYTHONPATH=d:\Code\myproject
$> export DJANGO_SETTINGS_MODULE=myapp.settings.dev_settings
$> python.exe myapp/manage.py syncdb

I know the versions are not the same, but I'm not sure that the minor differences may cause all my woe. Moreover I don't seem to find the exact same python version for Windows.

Any thoughts? Thanks a lot for reading.

O.

EDIT: added the manage.py content

#!/usr/bin/env python
from django.core.management import execute_manager
import os


    if __name__ == "__main__":
    settings = None
    try:
        if os.environ.has_key('LOCAL_SERVER_SETTINGS'):
            os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings.%s' % os.environ['LOCAL_SERVER_SETTINGS']

        if os.environ.has_key('DJANGO_SETTINGS_MODULE'):
            settings = __import__(os.environ['DJANGO_SETTINGS_MODULE'])

        if settings is None:
            import settings
        execute_manager(settings)

    except ImportError:
        import sys
        sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)

        import traceback
        traceback.print_exc()

        sys.exit(1)

EDIT : more on what happens in the myapppackage

I patch some django functions/classes from within the myapp.__init__ module. I was thinking the import django part in this module was causing a circular reference. The code is executed when I load myapp.settings.[any_config] and could have caused the crash. But then, how come the correct settings module is loaded with no error by WSGI, and that it works fine also on Windows? More : after commenting out the said code parts, the ImportError is still there.

1

There are 1 best solutions below

6
On

If you move your settings file, you need to modify manage.py to tell it where to find it. The default is for it to be in the same directory as manage.py, but if you move into another python module(folder with __init__.py) then you need to update the reference in manage.py.

Look in manage.py where it imports your settings module, and import settings.dev_settings in your case instead. Look for 2 important lines

imp.find_module('settings') # Assumed to be in the same directory.
...
import settings

Change these to reference where you moved the settings file to(assuming you moved to settings.dev_settings):

imp.find_module('settings.dev_settings') # Assumed to be in the same directory.
...
from settings import dev_settings as settings

You can also use the command line option --settings to specify which settings file to use.