Python django.core.exceptions.ImproperlyConfigured:请求设置缓存,但未配置设置。您必须定义环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24011428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment varia
提问by Maximas
I've tried everything that I could find to fix this issue, and I'm starting to tear my hair out a little. I'm getting this error:
我已经尝试了我能找到的所有方法来解决这个问题,但我开始有点扯掉我的头发。我收到此错误:
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.
My scripts run fine when I do:
当我这样做时,我的脚本运行良好:
python3 ./manage.py runserver
However, whenever I try to run tests, I get the above error... I use a VirtualEnv, that inherits nothing globally, everything is installed (with the correct version), and within my manage.py I have set:
但是,每当我尝试运行测试时,都会出现上述错误......我使用了一个 VirtualEnv,它在全局范围内没有继承任何东西,安装了所有东西(使用正确的版本),并且在我的 manage.py 中我设置了:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.settings")
I'm using PyCharm Proffesional to develop, and I've tried running the tests in both the IDE, and in the shell. Within the shell I'm using :
我正在使用 PyCharm Proffesional 进行开发,并且尝试在 IDE 和 shell 中运行测试。在我使用的外壳中:
python3 manage.py test
The shell is finding no tests. The test is basic, and I'm not really all that bothered about the content of it currently as it's the environment I'm struggling with. UPDATE: I have solved the issue with the shell. Tests must be defined by:
外壳未找到任何测试。测试是基本的,我现在并不真正关心它的内容,因为它是我正在努力的环境。更新:我已经解决了 shell 的问题。测试必须由以下定义:
def test_<name>():
However this hasn't solved my issue with PyCharm. I have also called:
然而,这并没有解决我的 PyCharm 问题。我也打过电话:
settings.configure()
Which told me that it was already configured.
Please note that I am notusing any database with Django, and I have commented the appropriate things out of the settings.
这告诉我它已经配置好了。
请注意,我没有在 Django 中使用任何数据库,并且我已经从设置中注释了适当的内容。
The full error is:
完整的错误是:
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.
I have ran PyCharm under sudoto ensure that the issue wasn't permissions as I'm storing the env under root.
我在sudo下运行了 PyCharm以确保问题不是权限,因为我将 env 存储在 root 下。
EDIT: I've just discovered that my tests which do not use Django are running fine, but Pycharm is still throwing several failures. These failures aren't the individual tests, they're just the error which I have mentioned here (there's 341 tests that aren't Django related). I only have one test which uses Django, which will not get past initializing and throwing the mentioned error.
编辑:我刚刚发现我的不使用 Django 的测试运行良好,但 Pycharm 仍然出现多次失败。这些失败不是个别测试,它们只是我在这里提到的错误(有 341 个测试与 Django 无关)。我只有一个使用 Django 的测试,它不会通过初始化和抛出提到的错误。
Hope I've been explanitory
希望我一直在解释
采纳答案by yehe
If you are using PyCharm Pro, you can either test your app by action 'Run Django Console...'. After you click 'test', it will prompt you for the app you want to test.
如果您使用的是 PyCharm Pro,您可以通过操作“运行 Django 控制台...”来测试您的应用程序。单击“测试”后,它会提示您选择要测试的应用程序。
or
或者
Create a Django test in Run/Debug configuration.
在运行/调试配置中创建一个 Django 测试。
回答by Steve Bradshaw
I got this error when I imported TestCase from unittest2. I fixed it by importing TestCase by:
当我从 unittest2 导入 TestCase 时出现此错误。我通过以下方式导入 TestCase 修复了它:
from django.test import TestCase
My suspicions were raised when the test was labelled "Unittest" rather than "Test". I had to run the whole suite before Pycharm realised I'd made this change...
当测试被标记为“Unittest”而不是“Test”时,我产生了怀疑。在 Pycharm 意识到我做了这个改变之前,我不得不运行整个套件......
回答by Vishal Sharma
Use this
用这个
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
instead of
代替
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.settings")
回答by Hijane
In your python script, you are trying to access Django models before setting the environment try it in this order :
在您的 python 脚本中,您尝试在设置环境之前访问 Django 模型,请按以下顺序尝试:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.`settings`")
from <appname>.models import Class1, Class2,...
回答by guettli
since, I don't want to add this in every file, I created a file called usercustomize.py and added this content:
因为,我不想在每个文件中添加它,我创建了一个名为 usercustomize.py 的文件并添加了以下内容:
import os
os.environ['DJANGO_SETTINGS_MODULE']='foosite.settings'
The file path in my case:
在我的情况下的文件路径:
src/foosite/usercustomize.py
I installed foosite with "pip install -e". I am unsure if this works for normal installs.
我用“pip install -e”安装了foosite。我不确定这是否适用于正常安装。
回答by Josh
回答by Samer s Salib
In my case I needed to use
就我而言,我需要使用
python3 manage.py check --deploy
instead of
代替
django-admin check --deploy