【问题标题】:django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configureddjango.core.exceptions.ImproperlyConfigured:请求设置 LOGGING_CONFIG,但未配置设置
【发布时间】:2015-04-16 01:01:55
【问题描述】:

我正在尝试运行我从 tango_with_django 教程 (https://github.com/leifos/tango_with_django/blob/master/tango_with_django_project/populate_rango.py) 中汇总的填充脚本,但是我得到了以下回溯,它似乎与 Django 1.7 中所做的更改有关?如果有人能解释我在这里做错了什么,我将不胜感激。

(test_env) C:\Users\WriteCode\test_env\epl>python populate_clubs.py
Traceback (most recent call last):
  File "populate_clubs.py", line 4, in <module>
    django.setup()
  File "c:\Python27\lib\site-packages\django\__init__.py", line 20, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 46, in __ge
tattr__
    self._setup(name)
  File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _set
up
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, b
ut settings are not configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

(test_env) C:\Users\WriteCode\test_env\epl>

我的 populate_clubs.py 脚本

import os
import sys
import django
django.setup()


def add_club(name, nickname, manager, established, stadium, active=True):
    c = clubs.objects.get_or_create(name=name, nickname=nickname, manager=manager, established=established, stadium=stadium, active=active)
    return c

def populate():
    add_club(name = "Aston Villa",
        nickname = "Villans",
        manager = "Tim Sherwood",
        established = "1897",
        stadium = "Villa Park"
        )

    # Print out what was added
    for c in clubs.objects.all():
        print "The following has been added to clubs:" + c



# Start execution
if __name__ == '__main__':
    print "Starting club population script..."
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings')
    from teams.models import clubs
    populate()

【问题讨论】:

  • 使用管理命令可能是使用脚本与 django 交互的最简单方法

标签: python django


【解决方案1】:

调用django.setup() 应该在设置DJANGO_SETTINGS_MODULE 环境变量之后进行。只需将其移至您的__main__,紧跟在os.environ.setdefault() 之后即可。

【讨论】:

    【解决方案2】:

    如果您正在运行 PyCharm,对我有用的是使缓存无效并重新启动应用程序。

    File =&gt; Invalidate Caches / Restart ...

    我的 virtualenv 最近已从 Python 3.6 或 3.7 更新到 3.8。

    【讨论】:

      【解决方案3】:
      os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings')
      

      应该高于

      import django
      

      在导入和调用 django setup 之前你应该调用 django_settings_module。

      希望这有帮助。

      【讨论】:

      • settings.py?
      【解决方案4】:

      我在设置环境时遇到了同样的问题。

      (我尝试添加:

      os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
      

      进入“启动脚本窗口”,在Django Console中或者重写.wsgi文件,但是都失败了)

      我的最终解决方案:(打开终端,手动设置环境)

      这里是步骤:(对于mac)

      1. 打开终端并输入 vim ~/.bash_profile

      2. 添加以下行:

         PATH=${PATH}:/Users/work/workspace/[virtual_environment]/bin
         PYTHONPATH=${PATH}:/Users/work/PycharmProjects/[project_name]
         DJANGO_SETTINGS_MODULE=[project_name].settings
        
      3. 输入 :wq 保存退出终端

      4. 键入 source ~/.bash_profile 以重新加载它

      这次运行python manage.py shell 就可以了。

      【讨论】:

      • 这适用于生产环境,还是仅适用于本地开发机器?
      【解决方案5】:

      当我在非 django 模块中使用 django 相关的 import 语句时,发生了这种情况。
      from index.views import view 确实给我带来了错误。

      【讨论】:

        【解决方案6】:

        开发和调试可以使用standalonepython包。

        使用 pip 安装

        pip install standalone
        

        使用standalone来使用模块中的Django。

        # add the following to the top of the module.
        import standalone
        standalone.run('mysite.settings') # replace with your settings module.
        
        # your code goes below the configuration
        import os
        import sys
        
        # ... .. .  whole module
        

        现在您可以将模块作为 Python Django 脚本运行。

        【讨论】:

          【解决方案7】:

          如果您通过在终端中运行 python 启动与 Django 的交互后遇到类似错误,则在相应目录中运行 python manage.py shell 可能会解决您的问题。

          【讨论】:

          • 我忘了 Django 有自己的交互式 shell。这为我解决了!
          • 为什么会有帮助?
          【解决方案8】:

          您可以在django.setup() 行之前插入os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

          【讨论】:

          • 我在使用 PyCharm(版本 2017.2.3)作为我的 IDE 时遇到了同样的问题。要解决这个问题是 PyCharm,你需要去PyCharm -&gt; Settings -&gt; Build, Execution, Deployment -&gt; Console -&gt; Django Console。然后在Starting script窗口中输入os.environ.setdefault(...)
          • PyCharm -&gt; Settings -&gt; Build, Execution, Deployment -&gt; Console -&gt; Django Console 中有一个专用按钮来设置Environment variables。打开并添加DJANGO_SETTINGS_MODULE = some_project.settings
          猜你喜欢
          • 2021-10-04
          • 2018-05-21
          • 2020-04-12
          • 2021-08-07
          • 1970-01-01
          • 1970-01-01
          • 2020-08-25
          • 2014-07-23
          • 2020-09-27
          相关资源
          最近更新 更多