python config_error ai_generated true

pytest.PytestConfigWarning: 未知配置项: DJANGO_SETTINGS_MODULE E django.core.exceptions.ImproperlyConfigured: 请求设置 INSTALLED_APPS,但设置未配置。

pytest.PytestConfigWarning: Unknown config option: DJANGO_SETTINGS_MODULE E django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured.

ID: python/pytest-django-settings-not-configured

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-08-05首次发现

版本兼容性

版本状态引入弃用备注
4.x active — — —

根因分析

pytest-django 未安装或未启用,导致 pytest.ini 中的 DJANGO_SETTINGS_MODULE 被忽略,Django 在导入前未配置。

English

pytest-django is not installed or not active, so DJANGO_SETTINGS_MODULE in pytest.ini is ignored and Django isn't configured before import.

generic

解决方案

  1. 95% 成功率
    pip install pytest-django, then in pyproject.toml:
    [tool.pytest.ini_options]
    DJANGO_SETTINGS_MODULE = "myproj.settings"
    python_files = "tests.py test_*.py *_tests.py"

无效尝试

常见但无效的做法:

  1. 60% 失败

    Works only if `django.setup()` is also called; still fails on fixtures.

  2. 55% 失败

    pytest-django does this itself; double-init causes warnings and test DB issues.