23

I want to use a different settings file in django -- specifically settings_prod -- yet whenever I try to do a syncdb with --settings=settings_prod, it complains:

python2.6 manage.py syncdb  --settings=settings_prod
Error: Can't find the file 'settings.py' in the directory containing '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.)

I've also tried setting the environment variable DJANGO_SETTINGS_MODULE=settings_prod to no end.

Edit: I have also set the environment variable in my wsgi file, also to no end:

import os
import sys

from django.core.handlers.wsgi import WSGIHandler

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings_prod'
application = WSGIHandler()

Suggestions?

Brian D
  • 9,863
  • 18
  • 61
  • 96
  • 1
    And with the WSGI is it working correctly? I had this problem before, but only with `manage.py` – pajton Mar 29 '11 at 00:36
  • I think so, because it's at least displaying a URL that I set. I just can't get it to syncdb. – Brian D Mar 29 '11 at 00:38
  • 1
    Ok, someone just answered what I had in mind:). `import settings` is hardcoded in `manage.py` so no luck with this simple approach. – pajton Mar 29 '11 at 00:40

5 Answers5

24

Try creating a settings module.

  1. Make a settings folder in the same directory as manage.py.
  2. Put your different settings files in that folder (e.g. base.py and prod.py).
  3. Make __init__.py and import whatever settings you want to use as your default. For example, your __init__.py file might look like this:

    from base import *
    
  4. Run your project and override the settings:

    $ python2.6 manage.py syncdb --settings=settings.prod
    
Elliot Cameron
  • 5,235
  • 2
  • 27
  • 34
14

I do know that no matter what you do with manage.py, you're going to get that error because manage.py does a relative import of settings:

try:
    import settings # Assumed to be in the same directory.

http://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-option---settings

Note that this option is unnecessary in manage.py, because it uses settings.py from the current project by default.

You should try django-admin.py syncdb --settings=mysettings instead

Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • 1
    I have tried that, but I get: `ImportError: Could not import settings 'settings_prod' (Is it on sys.path?): No module named settings_prod` -- but it should be, according to my WSGI file – Brian D Mar 29 '11 at 00:41
  • Well, actually I just modified the WSGI file to include this: `sys.path.append("/home/username/webapps/django/project")`, but still no bueno. – Brian D Mar 29 '11 at 00:46
  • try passing the --pythonpath argument to django-admin.py to ensure that your path is set up correctly. – Wogan Mar 29 '11 at 00:47
  • 3
    Regardless of what your WSGI config file says, if you're running django-admin.py your settings file has to be on the pythonpath _now_, not when your WSGI handler is called by Apache. You could for example edit your manage.py file and do the appropriate `sys.path` modifications and change the hard coded `import settings` to `import mysettings as settings` – Yuji 'Tomita' Tomita Mar 29 '11 at 00:50
4

this works for me:

DJANGO_SETTINGS_MODULE=config.settings.abc python manage.py migrate
Charlie 木匠
  • 2,234
  • 19
  • 19
1

this will help you:

  • create a another file "setting_prod.py" with your original settings.py file.

  • write down your setting which you need to run, in setting_prod.py file.

  • Then import setting_prod.py file in your settings.py file.

for ex. settings.py:

VARIABLE = 1

import setting_prod

setting_prod.py

VARIABLE = 2

After importing setting_prod.py file in settings.py file, VARIABLE will set to new value to "2" from "1".

PythonDev
  • 4,287
  • 6
  • 32
  • 39
  • why you have .py in the end? I've got error `ImportError: No module named py` and VARIABLE string won't be replaced as you want. If you'll have `import setting_prod` it will be as setting_prod.VARIABLE – holms Sep 04 '13 at 05:16
  • If you set `from setting_prod import *` You will get the behavior you are looking for but, it is less pythonic. – IamnotBatman Mar 18 '14 at 03:42
0

We can use this method to set different settings file, for example, I use different settings file for my unit test (settings_unit_test.py). Also I do have other settings file for different infrastructure environment settings_dev.py, settings_test.py and settings_prod.py.

In windows environment(same can done in linux as well)

set DJANGO_SETTINGS_MODULE=settings_unit_test
set PYTHONPATH=<path_of_your_directory_where_this_file_'settings_unit_test.py'_is_kept>
Sathish
  • 51
  • 2