【发布时间】:2014-07-23 13:38:11
【问题描述】:
我已经尝试了所有能找到的方法来解决这个问题,但现在我的头发开始掉了一点。 我收到此错误:
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.
当我这样做时,我的脚本运行良好:
python3 ./manage.py runserver
但是,每当我尝试运行测试时,都会收到上述错误... 我使用了一个 VirtualEnv,它不会在全局范围内继承任何东西,所有东西都已安装(使用正确的版本),并且在我的 manage.py 中我已经设置了:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<appname>.settings")
我正在使用 PyCharm Proffesional 进行开发,并尝试在 IDE 和 shell 中运行测试。 在我使用的外壳中:
python3 manage.py test
shell 没有找到任何测试。测试是基本的,我目前并不关心它的内容,因为这是我正在努力解决的环境。 更新:我已经解决了 shell 的问题。测试必须由以下人员定义:
def test_<name>():
但这并没有解决我的 PyCharm 问题。 我也打过电话:
settings.configure()
告诉我它已经配置好了。
请注意,我不在 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.
我已在 sudo 下运行 PyCharm,以确保问题不是权限,因为我将 env 存储在 root 下。
编辑:我刚刚发现我的不使用 Django 的测试运行良好,但 Pycharm 仍然出现了几次失败。这些失败不是单独的测试,它们只是我在这里提到的错误(有 341 个测试与 Django 无关)。我只有一个使用 Django 的测试,它不会通过初始化并抛出上述错误。
希望我一直在解释
【问题讨论】:
-
以
sudo的身份运行是答案 - 当问题是“我怎样才能弄乱我的主文件夹的权限”时。
标签: python django python-3.x pycharm