问题描述
我已经尝试了一切可以找到解决这个问题的东西,而且我开始把头发撕开了。
我收到此错误:
django.core.exceptions.ImproperlyConfigured:请求设置CACHES,但设置为未配置在访问设置之前,您必须定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。
我的脚本运行正常:
python3 ./manage.py runserver
然而,我试图运行测试,我得到上面的错误...
我使用一个VirtualEnv,它不继承全局,一切都安装(使用正确的版本),在我的manage.py我已经设置: p>
os.environ.setdefault(DJANGO_SETTINGS_MODULE,< appname> .settings)
我正在使用PyCharm Proffesional开发,我已经尝试在IDE和shell中运行测试。
在我使用的shell中:
python3 manage.py test
shell找不到任何测试。测试是基本的,而我现在并不是很在乎目前的内容,因为它是我正在努力的环境。 更新:我已经解决了shell的问题。测试必须定义为:
def test_< name>():
/ pre>
然而,这还没有解决我的问题与PyCharm。
我也叫:settings.configure()
/ pre>
其中告诉我已经配置了。
请注意,我不使用任何数据库与Django,我已经评论了相关的设置。
完整的错误是:
追溯(最近的最后一次呼叫):
文件/root/kiloenv/lib/python3.4/site-packages/django/conf/__init__.py,第38行,在_setup
settings_module = os.environ [ENVIRONMENT_VARIABLE]
文件/usr/lib/python3.4/os.py,第631行,__getitem__
将来自None的KeyError(key)
KeyError:'DJANGO_SETTINGS_MODULE'
在处理上述异常时,发生另一个异常:
追溯(最近的最后一次调用):
文件 /home/<username>/Documents/<dirname>/<appname>/tests.py,第一行在< module>
from django.test.utils import setup_test_environment
文件/root/virtualenv/lib/python3.4/site-packages/django/test/__init__.py,第5行在< module>
from django.test.client import Client,RequestFactory
文件/root/virtualenv/lib/python3.4/site-packages/django/test/client.py,第11行,<模块>
from django.contrib.auth import authenticate,login,logout,get_user_model
文件/root/virtualenv/lib/python3.4/site-packages/django/contrib/auth/__init__.py,第6行,<模块>
from django.middleware.csrf import rotate_token
文件/root/virtualenv/lib/python3.4/site-packages/django/middleware/csrf.py,第14行在< module>
from django.utils.cache import patch_vary_headers
文件/root/virtualenv/lib/python3.4/site-packages/django/utils/cache.py,第26行在< module>
from django.core.cache import get_cache
文件/root/virtualenv/lib/python3.4/site-packages/django/core/cache/__init__.py,第69行,模块>
如果DEFAULT_CACHE_ALIAS不在settings.CACHES中:
文件/root/virtualenv/lib/python3.4/site-packages/django/conf/__init__.py,第54行,__getattr__
self._setup(name)
文件/root/virtualenv/lib/python3.4/site-packages/django/conf/__init__.py,第47行,_setup
%(desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured:请求的设置CACHES,但没有配置设置。在访问设置之前,您必须定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。
我已经在 sudo 下运行PyCharm,以确保问题不是权限,因为我正在根目录下存储env。
编辑:我刚刚发现我的测试不使用Django正在运行好的,但是Pycharm仍然抛出了几个失败。这些失败不是单独的测试,它们只是我在这里提到的错误(有341个测试不是Django相关的)。我只有一个使用Django的测试,不会超过初始化并抛出提到的错误。
希望我已经解释了
解决方案如果您使用PyCharm Pro,您可以通过操作运行Django控制台...来测试应用程序。点击测试后,它将提示您输入要测试的应用程序。
或
创建一个Django测试运行/调试配置。
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:
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 :
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:
def test_<name>():
However this hasn't solved my issue with PyCharm.I have also called:
settings.configure()
Which told me that it was already configured.
Please note that I am not using any database with Django, and I have commented the appropriate things out of the settings.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 sudo to ensure that the issue wasn't permissions as I'm storing the env under 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.
Hope I've been explanitory
解决方案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.
or
Create a Django test in Run/Debug configuration.
这篇关于django.core.exceptions.ImproperlyConfigured:请求设置CACHES,但没有配置设置。您必须定义环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!