2

尝试使用 Python Tool for Visual Studio 运行 shell 命令时收到以下错误。我已将数据库添加到设置文件中,并且能够运行 django 应用程序而没有错误,但是当我尝试使用 shell 添加数据时,它会抛出此错误:

>>> from ProjectTrackerServer.projects.models import Project
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\djangoapps\ProjectTrackerServer\ProjectTrackerServer\projects\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.
4

1 回答 1

1

该错误说明了问题。这是因为您忘记提供项目设置 DJANGO_SETTINGS_MODULE。您可以通过提供它来修复它。您需要设置 DJANGO_SETTINGS_MODULE 环境变量。

When you use Django, you have to tell it which settings
you're using. Do this by using an environment variable, 
DJANGO_SETTINGS_MODULE.

The value of DJANGO_SETTINGS_MODULE should be in 
Python path syntax, e.g. mysite.settings. Note 
that the settings module should be on the 
Python import search path.

https://docs.djangoproject.com/en/dev/topics/settings/

于 2013-02-04T17:09:48.297 回答