1

I have recently started working with a settings directory as described in the Two Scoops of Django book. It contains the following files

  • local.py
  • staging.py
  • production.py
  • test.py
  • __init__.py

To be able to use the different setting files on the server I have adapted my django.fcgi script to import the settings module. It works very smoothly.

How do I do the same on my local machine on which I use runserver, however? I have set the DJANGO_SETTINGS_MODULE and I have adapted the manage.py file to

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

import sys
sys.path.insert(0, '/home/user/don.joey/projects/a_project/a_project_site/settings')

import settings

try:
    imp.find_module('settings') # Assumed to be in the same directory.
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" % __file__)
    sys.exit(1)

if __name__ == "__main__":
    execute_manager(settings)

It works fine.

How can I make django-admin.py find these settings? I do not want to manually edit django-admin.py because it is part of my virtualenv and it will does thus regularly be updated.

Update

I have set the following: export DJANGO_SETTINGS_MODULE=settings.local.

Community
  • 1
  • 1
Private
  • 2,626
  • 1
  • 22
  • 39
  • Why not have the virtualenv start script set `$DJANGO_SETTINGS_MODULE` to `settings.whatever`? – Ignacio Vazquez-Abrams Dec 04 '13 at 19:56
  • Have you add `DJANGO_SETTINGS_MODULE` to your `env` AND the project root to your `PYTHONPATH`? – bnjmn Dec 04 '13 at 19:56
  • @bnjmn I have set `DJANGO_SETTINGS_MODULE`, but I have no idea where to set the pythonpath for the project root. Is that in my venv? – Private Dec 04 '13 at 19:57
  • Add it to your shell env, if that's where you are using `django-admin.py`, something like `export PYTHONPATH=/home/user/don.joey/projects/a_project/a_project_site:PYTHONPATH` – bnjmn Dec 04 '13 at 19:59
  • It should be the directory where the actual `project_root` is. It should be wherever the `manage.py` file is. – bnjmn Dec 04 '13 at 20:01
  • @bnjmn Thanks for your help. I think we are almost there. I have tried running the command `PYTHONPATH=/home/user/don.joey/projects/a_project/a_project_site/ django-admin.py shell` but it does not work – Private Dec 04 '13 at 20:11

1 Answers1

2

You need to set two things

  • DJANGO_SETTINGS_MODULE and
  • PYTHONPATH so that the settings module can be found

The way Two Scoops of Django suggests setting up a project named blah you would have the following directory structure:

- blah_project/
    - blah/
        - manage.py
        - blah/
            - ...
            - settings/
                 - __init__.py
                 - local.py
                 - production.py
                 - ...

Run the following (assuming a bash environment):

export DJANGO_SETTINGS_MODULE=blah.settings.local
export PYTHONPATH=/full/path/to/blah_project/blah

As long as django-admin.py is on your path (and it should be if django is installed and activated within your venv), you should be able to run:

django-admin.py runserver
bnjmn
  • 4,508
  • 4
  • 37
  • 52
  • This may not be exactly what you are used to if you are using `./manage.py` to run your commands but it may work a little better actually. You can then easily switch settings by simply change the `DJANGO_SETTINGS_MODULE`. This makes (staging|production) deployment much easier. Also, no need to edit `manage.py`. – bnjmn Dec 04 '13 at 20:27
  • This if nice and it works. One more thing: could you add something on how to set these variables automatically when activating a venv? – Private Dec 04 '13 at 20:30
  • Glad to hear it. I can add that. If the answer solved your problem Could you up vote ;)? Are you using `virtualenv-wrapper` by any chance? It makes that process pretty simple through a `postactivate` hook. http://stackoverflow.com/a/11134336/770019 – bnjmn Dec 04 '13 at 20:33
  • Done. I'll look into the link of your comment. Thakns – Private Dec 04 '13 at 20:37