使用pycharm未定义DJANGO_SETTINGS_MODULE

时间:2013-01-20 18:08:27

标签: django pycharm

我有DJANGO_SETTINGS_MODULE问题。我正在使用Pycharm并在项目设置下 - > Django支持所有设置和启用。然而,我在models.py中尝试导入时出现以下错误:

from django.db import models

C:\Python27\python.exe C:/Users/Grimbo/PycharmProjects/Muspy/poll/models.py
Traceback (most recent call last):
  File "C:/Users/Grimbo/PycharmProjects/Mus/poll/models.py", line 1, in <module>
    from django.db import models
  File "C:\Python27\lib\site-packages\django\db\__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 184, in inner
    self._setup()
  File "C:\Python27\lib\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.

打印(sys.path中):

['C:\\Program Files (x86)\\JetBrains\\PyCharm 2.6.3\\helpers\\pydev', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Users\\Grimbo\\PycharmProjects\\Mus']

有人知道出了什么问题吗?

2 个答案:

答案 0 :(得分:8)

Check out this link。具体来说,您要在配置中设置Environmental Variable。默认情况下,您会看到PYTHONBUFFERED = 1,并且您需要添加DJANGO_SETTINGS_MODULE = project.settings - 显然会将project替换为项目的实际名称。

另一件好事是转到File-> Settings -> Django Support ->并确保您的Django Root,Settings.py和Manage.py字段正确无误。

答案 1 :(得分:4)

以下Dan Hoerst的回答对我有用,但不清楚在哪里设置环境变量。您可以在菜单选项下找到该设置:

Run -> Edit Configurations...

展开左侧的Django Server选项,然后选择您的项目。在那里,您会找到Environment Variables:设置。请记住使用像Muspy.settings这样的虚线路径而不是文件路径。

丹,我会回复你的回答,但没有代表。