Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. January 25, 2010

Filed under: Django,Error,Python — Rupert Bates @ 12:14 pm

If you get the following error when trying to use a Django package from the interactive prompt in Pydev:
“ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.”

You can solve it by setting the required environment variable in the __init__.py file of your app as follows:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
Advertisements
 

17 Responses to “ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.”

  1. Elliott Goodwin Says:

    I was working with pydev and apatana. I ran across this variable problem, but your fix worked great. Thanks.

  2. Paolo Says:

    Perfect! Thanks

  3. Rasheed Says:

    Thanks man… Great job

  4. jo Says:

    i added this and it doesn’t work…its now giving me more issues….Error: Could not import settings ‘mysite.settings’ (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings

  5. derek Says:

    Same problem as jo … I have tried “myapp.settings” and “myproj.settings” and get the same error. This is happening in a subdirectory nested inside the app.

  6. mithuna Says:

    I am trying to add PyDev to eclipse by following this page http://pydev.org/manual_adv_django.html and i am not able to set the DJANGO_SETTINGS_MODULE value correctly. I get the error

    from django.core import management;import .settings as settings;management.setup_environ(settings)
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named .settings

    • mithuna Says:

      the actual error is below.

      from django.core import management;import myapp.settings as settings;management.setup_environ(settings)
      Traceback (most recent call last):
      File “”, line 1, in
      ImportError: No module named myapp.settings

      • Rupert Bates Says:

        ‘myapp.settings’ in the above code is just an example. You need to change it to the settings module of your application.
        See here for more info

  7. Ben Gdovicak Says:

    Make sure that “myapp” is in your python path or it may not be able to find myapp.settings.

  8. Chris Sullivan Says:

    Shouldn’t that be “mysite” rather than “myapp”? Anyway that’s what works for me.

  9. Gabriel Says:

    Hey guys! If you’re having this problem, one easy solution might be to use the ‘python manage.py shell’ command and let the django shell handle environment variables for you! I’m just starting out, but that helped me. Better to always use the django shell and let it deal with the messy bits, that’s what I’ve learned over the last five minutes 😛

  10. Filix Says:

    Thanks ^^ ^^

  11. gratulations Says:

    maybe you’re using an old version of eclipse, but there is actually a configuration property where you put in your settings name, you don’t need to change your code

  12. […] ImportError: Settings cannot be imported, because … – Jan 25, 2010 · 15 Responses to “ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.”… […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s