django.core.exceptions.ImproperlyConfigured:请求设置CACHES,但未配置设置.您必须定义环境变量

Max*_*mas 28 python django pycharm python-3.x

我已经尝试了一些我能找到解决这个问题的东西,而且我开始把头发撕掉一点.我收到这个错误:

django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Run Code Online (Sandbox Code Playgroud)

我做的时候我的脚本运行正常:

python3 ./manage.py runserver
Run Code Online (Sandbox Code Playgroud)

但是,每当我尝试运行测试时,我都会收到上述错误...我使用VirtualEnv,它不会全局继承,所有内容都安装(使用正确的版本),并且在我的manage.py中设置了:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.settings")
Run Code Online (Sandbox Code Playgroud)

我正在使用PyCharm Proffesional开发,我已经尝试在IDE和shell中运行测试.在我正在使用的shell中:

python3 manage.py test     
Run Code Online (Sandbox Code Playgroud)

shell没有找到测试.测试是基本的,我并不是真的对目前的内容感到困扰,因为这是我正在努力的环境.更新:我已经解决了shell的问题.测试必须通过以下方式定义:

def test_<name>():
Run Code Online (Sandbox Code Playgroud)

然而,这并没有解决我与PyCharm的问题.我也打过电话:

settings.configure()
Run Code Online (Sandbox Code Playgroud)

哪位告诉我它已经配置好了.
请注意,我没有使用任何带Django的数据库,我已经在设置中评论了相应的内容.

完整的错误是:

  Traceback (most recent call last):
  File "/root/kiloenv/lib/python3.4/site-packages/django/conf/__init__.py", line 38, in _setup
    settings_module = os.environ[ENVIRONMENT_VARIABLE]
  File "/usr/lib/python3.4/os.py", line 631, in __getitem__
    raise KeyError(key) from None
KeyError: 'DJANGO_SETTINGS_MODULE'

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/home/<username>/Documents/<dirname>/<appname>/tests.py", line 1, in <module>
        from django.test.utils import setup_test_environment
      File "/root/virtualenv/lib/python3.4/site-packages/django/test/__init__.py", line 5, in <module>
        from django.test.client import Client, RequestFactory
      File "/root/virtualenv/lib/python3.4/site-packages/django/test/client.py", line 11, in <module>
        from django.contrib.auth import authenticate, login, logout, get_user_model
      File "/root/virtualenv/lib/python3.4/site-packages/django/contrib/auth/__init__.py", line 6, in <module>
        from django.middleware.csrf import rotate_token
      File "/root/virtualenv/lib/python3.4/site-packages/django/middleware/csrf.py", line 14, in <module>
        from django.utils.cache import patch_vary_headers
      File "/root/virtualenv/lib/python3.4/site-packages/django/utils/cache.py", line 26, in <module>
        from django.core.cache import get_cache
      File "/root/virtualenv/lib/python3.4/site-packages/django/core/cache/__init__.py", line 69, in <module>
        if DEFAULT_CACHE_ALIAS not in settings.CACHES:
      File "/root/virtualenv/lib/python3.4/site-packages/django/conf/__init__.py", line 54, in __getattr__
        self._setup(name)
      File "/root/virtualenv/lib/python3.4/site-packages/django/conf/__init__.py", line 47, in _setup
        % (desc, ENVIRONMENT_VARIABLE))
    django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Run Code Online (Sandbox Code Playgroud)

我在sudo下运行PyCharm 以确保问题不是权限,因为我将env存储在root下.

编辑:我刚刚发现我的测试不使用Django运行良好,但Pycharm仍然抛出几个失败.这些失败不是单独的测试,它们只是我在这里提到的错误(有341个测试与Django无关).我只有一个使用Django的测试,它不会超过初始化并抛出上述错误.

希望我一直都是解释性的

小智 34

用这个

import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
Run Code Online (Sandbox Code Playgroud)

代替

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.settings")
Run Code Online (Sandbox Code Playgroud)


小智 10

在您的python脚本中,您尝试在设置环境之前访问Django模型,请按以下顺序尝试:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.`settings`")
from <appname>.models import Class1, Class2,...
Run Code Online (Sandbox Code Playgroud)


yeh*_*ehe 9

如果您使用的是PyCharm Pro,您可以通过"Run Django Console ..."操作来测试您的应用.单击"测试"后,它将提示您输入要测试的应用程序.

在此输入图像描述

要么

在运行/调试配置中创建Django测试.

在此输入图像描述