0

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.

Olivier H
  • 835
  • 2
  • 8
  • 26
  • Did you add your `__init__.py` to settings folder? – nabucosound Jan 23 '12 at 17:09
  • What shell are you running? Is your environment variable getting saved? Check the output of the `env` command or `echo $DJANGO_SETTINGS_MODULE` – Kekoa Jan 23 '12 at 17:21
  • The `__init__` module exists in the settings package, and $DJANGO_SETTINGS_MODULE is indeed saved correctly. I can even print `os.environ['DJANGO_SETTINGS_MODULE']` while in python. – Olivier H Jan 23 '12 at 17:29
  • Added some details about what I do in the `myapp.__init__` module. – Olivier H Jan 24 '12 at 13:13

1 Answers1

0

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.

Kekoa
  • 27,892
  • 14
  • 72
  • 91
  • I've forgotten to say that someone has modifed the `manage.py` to import the module specified by the DJANGO_SETTINGS_MODULE env var, but as a `settings = __import__(os.environ['DJANGO_SETTINGS_MODULE'])`. Maybe not the best way to go? – Olivier H Jan 23 '12 at 17:32
  • Also, I see you are using a relative import from the app root (`settings.module`). Would it work with `myapp.settings.module`? – Olivier H Jan 23 '12 at 17:34
  • You can import in several ways in Python(django has some nice utils also), so I'm not concerned with how the import is done, just that it is targeting the right module, and that module exists. It's difficult to see what the problem is without knowing what customizations have been made to manage.py. – Kekoa Jan 23 '12 at 17:59
  • 1
    Have you tried to cd into the django folder and do `./manage.py` instead of from a level up? It could be your path isn't getting set properly? – Kekoa Jan 23 '12 at 18:00
  • 1
    Also, make sure you have an `__init__.py` in your django base folder. – Kekoa Jan 23 '12 at 18:00
  • I've added the content of `manage.py` in the original post. Also available here : http://pastebin.com/NPRTP9w9. – Olivier H Jan 24 '12 at 12:56