0

On the production server I would like to sync the db.

If I just did that

django-admin.py syncdb it would pick the settings.py. But I have an extra settings-production.py that inheritcs from settings.

Hence I came up with this:

django-admin.py syncdb --settings=/path/to/settings_production

ImportError: Could not import settings '/path/to/settings_production' (Is it on sys.path?): Import by filename is not supported.

I googled and found someone had a similar issue here. However something that confuses me is, the WSGI.py is only read by Apache. Not if I ran the command myself. The answer there doesn't really make sense to me.

How do I add my production_settings in sys.path manually to run syncdb? Many Thanks,

Community
  • 1
  • 1
Houman
  • 64,245
  • 87
  • 278
  • 460

1 Answers1

1

First make sure settings_production.py is in the same directory as settings.py.

Since you are inheriting from settings you can add at the bottom of settings.py:

try:
   from settings_production import *
except ImportError:
   pass

If you want to only use settings_production.py make sure it has all the settings for your application (database, et. al.) then:

DJANGO_SETTINGS_MODULE=settings_production python manage.py syncdb

Edit

I was going to post it as a comment, but it became too long:

I did it this way because typically in production one only changes a few values you can import in the master settings.py; and the last value of the variable is used (so if you set production db in settings_production.py, then that setting is used).

It is easier this way since django by default will look for settings.py; avoids having to deal with environment variables and import paths especially if you are changing minor things.

There are a few draw backs to this approach; for example I can't imagine its very portable especially if you have multiple environments (staging/testing/production/dev), as then you have multiple from _foo_ import * statements and it can lead to problems because the order of imports becomes significant; if you forgot and kept around a stray settings_*.py around - for example the test environment also got the dev file because some developer forgot to remove it from the test branch.

For more complex setups its easier to keep complete copies of the master settings.py file; and then point DJANGO_SETTINGS_MODULE to the python path of those files. The caveat here is that you have to make sure PYTHONPATH is adjusted to include the location of your settings.

For more ideas, see the SplitSettings wiki entry.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thanks Burhan, according to http://djangobook.com/en/2.0/chapter12/ under Using Different Settings for Production it is the settings_production.py that imports from settings `from settings import *` You seem to do it the other way around, no? – Houman Aug 06 '12 at 13:47