如何在DJANGO_SETTINGS_MODULE中设置环境变量

时间:2014-10-23 12:43:06

标签: django

from django.contrib.auth.models import User

如果我在终端上给出上面的编码,它会抛出错误。我正在使用ubuntu。我该如何设置环境变量?我使用的是python 2.7.6版本。

File "/usr/local/lib/python2.7/dist-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.

1 个答案:

答案 0 :(得分:0)

我曾经不得不这样做,以便在我做的项目中使数据库相关的测试工作:

import os
if not 'DJANGO_SETTINGS_MODULE' in os.environ or os.environ['DJANGO_SETTINGS_MODULE'] != '<myProject>.settings':
    os.environ['DJANGO_SETTINGS_MODULE'] = '<myProject>.settings'

from django.contrib.auth.models import User