【问题标题】:raise ImportError("Settings cannot be imported, because environment variable %s is undefinedraise ImportError("无法导入设置,因为环境变量 %s 未定义
【发布时间】:2013-03-01 19:35:35
【问题描述】:

Stack 上有几个关于这个错误的问题,但没有答案对我有帮助,奇怪的是这个错误突然出现了,而之前一切正常。

我所做的是重新启动服务器,突然出现这种情况:

为什么在我什么都没做的时候突然出现这个错误,我该如何解决?

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.

【问题讨论】:

  • 检查这个:python manage.py shell 然后import sys 然后sys.path。项目目录是否在该路径上?退出。输入常规 python shell python。然后import syssys.path。项目目录在那个路径上吗?
  • 您使用的是什么操作系统?也许您将DJANGO_SETTINGS_MODULE 设置为环境变量,在重新启动时清除。无论如何,您需要设置此变量。它通常在manage.py 中为您处理,但有些人不得不求助于手动设置。
  • @Cole:谢谢。项目目录只出现在python manage.py shell中,不出现在python shell中。

标签: django manage.py


【解决方案1】:

每当我打开一个新的 shell 时,我都会运行这个脚本:

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

更新:我稍微草率地编辑了脚本。所需要的只是:

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

【讨论】:

    【解决方案2】:

    转到您的项目文件夹,其中 manage.py 可用并运行:

    python manage.py shell

    运行通过错误的django语句。 (这里应该可以)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-24
      • 2012-07-14
      • 2017-05-04
      • 2020-03-19
      • 2021-02-20
      • 2019-05-19
      • 1970-01-01
      • 2018-04-24
      相关资源
      最近更新 更多