holyredbeard
holyredbeard

Reputation: 21218

raise ImportError("Settings cannot be imported, because environment variable %s is undefined

There are several questions here on Stack regarding this error, but no answer has helped me and the strange thing is that this error suddenly was raised while everything has worked fine before.

What I did was to restart the server, and suddenly this turned up:

How come this error suddenly can turn up while I haven't changed anything and how can I fix it?

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/bin/django-admin.py", line 5, in <module>
    management.execute_from_command_line()
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/core/management/__init__.py", line 69, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/core/management/commands/runserver.py", line 8, in <module>
    from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerException, get_internal_wsgi_application
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/core/servers/basehttp.py", line 26, in <module>
    from django.views import static
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/views/static.py", line 95, in <module>
    template_translatable = ugettext_noop(u"Index of %(directory)s")
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/utils/translation/__init__.py", line 75, in gettext_noop
    return _trans.gettext_noop(message)
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/utils/translation/__init__.py", line 48, in __getattr__
    if settings.USE_I18N:
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/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.

Upvotes: 3

Views: 6188

Answers (2)

Soorej P
Soorej P

Reputation: 531

Go to your project folder where manage.py is available and run:

python manage.py shell

run the django statements which through error. (It should work here)

Upvotes: 1

Cole
Cole

Reputation: 2509

I run this script whenever I open a new shell:

export PYTHONPATH=$PYTHONPATH:/Users/cole/Projects/Sites/my_project.com/my_project
export DJANGO_SETTINGS_MODULE=my_project.settings

UPDATE: I had edited the script in a little sloppily. All that is needed is:

export PYTHONPATH=$PYTHONPATH:/Path/To/Project/Root/Directory
export DJANGO_SETTINGS_MODULE=my_project.settings

Upvotes: 3

Related Questions