1

To be able to use the idle shell with django i am trying to set the DJANGO_SETTINGS_VARIABLE manually.I included the directory where the settings.py resides in the sys.path.and was able to import it.but the following doesn't seem to work .

  import settings
  from django.core.management import execute_manager
  execute_manager(settings)

I also tried

 settings = imp.find_module('settings')
 execute_manager(settings)

taking clue from manage.py but of no help. what's going on here , also how can a file be imported as a module , i remember a module needs to have a inti file inside a dir .

1
  • 1
    Instead of writing "it doesn't seem to work", specify what exactly is not working. Do you get an exception? What's the exception, then? Or does it run, but don't behave as you expected? If so, what exactly happens, and what did you expect should happen? Commented Jun 19, 2011 at 11:16

3 Answers 3

5

I had some difficulties with loading settings outside of the conventional usage scenario as well. I'm not 100% sure that I understand your difficulty here, so I'll give you a few possibilities and if you can clarify a bit, I'll try to help further.

If all you need is to use the idle shell, I think django is made to do this easily with:

python manage.py shell

assuming idle is your default shell (though I hope you'll check out ipython, if that's the case, since you'll probably enjoy the switch in the long run).

If you want to set the system variable for django, it's called: DJANGO_SETTINGS_MODULE, not DJANGO_SETTINGS_VARIABLE, or at least it was when I was using it. How you set the variable before launching shell will probably be platform specific. On linux you'd want to edit your .bashrc. On windows, you'd want to do a: "SET DJANGO_SETTINGS_MODULE=your.python.path.to.django.settings."

If you'd like to set the environment dynamically in a program before launching the shell that way, you can do, e.g.:

import settings
from django.core.management import setup_environ
setup_environ(settings)

Hope this helps; let me know if you need something else.

Sign up to request clarification or add additional context in comments.

Comments

1

You should be using configure()

from django.conf import settings
from myapp import myapp_defaults

settings.configure(default_settings=myapp_defaults, DEBUG=True)

1 Comment

What is myapp_defaults?
0

I'm sure there is a better way but so far this is the only way I got this working.

import django from django.conf import settings
from your_app import settings as app_settings

def get_our_settings():
    return {x: getattr(app_settings, x) for x in dir(app_settings) if x.isupper()}

if not settings.configured:
    settings.configure(**get_our_settings())
django.setup()

import pprint
pprint.pprint(dir(settings))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.