ImportError:环境变量DJANGO_SETTINGS_MODULE未定义

时间:2012-07-15 10:46:23

标签: python django django-admin

我正在阅读基本的django教程,我正在使用python manage.py runserver运行服务器。我得到这个错误,我不太了解如何理清:

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,在我添加了一些代码来向管理员注册一些应用模型后,我开始收到此消息。

提供更多信息:当我从项目目录中导出DJANGO_SETTINGS_MODULE =设置然后我执行python django-admin.py runserver时服务器不运行(没有显示任何内容)。

1 个答案:

答案 0 :(得分:8)

当您所在的位置不在项目根目录中时,您需要能够导入DJANGO_SETTINGS_MODULE的值。一种简单的方法是(1)将项目文件夹的父级添加到PYTHONPATH,(2)将DJANGO_SETTINGS_MODULE更改为projectfoldername.settings