Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: Environment variable DJANGO_SETTINGS_MODULE is undefined

I'm going through the basic django tutorial and I'm running the server using python manage.py runserver. And I get this error that I'm not quite understanding how to sort out:

Traceback (most recent call last):
  File "manage.py", line 1, in <module>
    from polls.models import Poll
  File "/Users/davidhaddad/Desktop/mysite/polls/models.py", line 2, in <module>
    from django.db import models
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

settings.py was visible earlier and I started getting this message after I added some code to register a few app models with admin.

To give some more info: when I do export DJANGO_SETTINGS_MODULE=settings from inside the project directory and then i do python django-admin.py runserver the server doesn't run (nothing is shown).

like image 914
David Haddad Avatar asked Jul 15 '12 10:07

David Haddad


1 Answers

You need to be able to import the value of DJANGO_SETTINGS_MODULE when you're in a location that isn't in your project root. One easy way of doing that is to (1) add the parent of your project folder to PYTHONPATH, (2) change DJANGO_SETTINGS_MODULE to projectfoldername.settings.

like image 107
thebjorn Avatar answered Sep 26 '22 23:09

thebjorn