【问题标题】:You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings您必须在访问设置之前定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure()
【发布时间】:2020-01-15 21:24:28
【问题描述】:

这是我的第一个 django 项目,如果我没有很好地解释自己,敬请见谅。我正在尝试编写一些单元测试。

此代码按预期工作:

from unittest import TestCase


class TestModel(TestCase):
    def test_CalculatePercentage(self):
        assert 1 == 1

但是,当我尝试从应用程序中导入某些内容时,即:

from investor.models import Investor

我收到ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. 错误:

Error
Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\unittest\case.py", line 60, in testPartExecutor
    yield
  File "C:\Program Files\Python38\lib\unittest\case.py", line 676, in run
    self._callTestMethod(testMethod)
  File "C:\Program Files\Python38\lib\unittest\case.py", line 633, in _callTestMethod
    method()
  File "C:\Program Files\Python38\lib\unittest\loader.py", line 34, in testFailure
    raise self._exception
ImportError: Failed to import test module: tests
Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\unittest\loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "C:\Program Files\Python38\lib\unittest\loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "C:\Users\lbrusokas\Documents\Dev\website\website_env\src\webapp\investor\tests.py", line 1, in <module>
    from investor.models import Investor
  File "C:\Users\lbrusokas\Documents\Dev\website\website_env\src\webapp\investor\models.py", line 2, in <module>
    from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
  File "C:\Program Files\Python38\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Program Files\Python38\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\Program Files\Python38\lib\site-packages\django\db\models\base.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Program Files\Python38\lib\site-packages\django\apps\registry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Program Files\Python38\lib\site-packages\django\apps\registry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Program Files\Python38\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
    self._setup(name)
  File "C:\Program Files\Python38\lib\site-packages\django\conf\__init__.py", line 38, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

This 是我的项目结构。

我看过很多关于这方面的帖子和建议,例如添加环境变量、调用 settings.setup()、配置 pycharm、启用 django 支持等,但对我没有任何帮助。

我还有哪些其他选项可以解决此问题,或者它是否发生在其他地方?

提前谢谢你!

【问题讨论】:

  • 您是在整个应用程序中还是在 tests.py 中都收到此错误?
  • 就在 tests.py 其他一切都按预期工作。
  • 等等,你是如何运行这个测试的?
  • 我使用Pycharm运行unittest实用程序(右键单击文档,在debug附近有一个选项可以运行单元测试)。但是,如果我通过终端(python manage.py test)运行,我会收到此错误`为别名'default'创建测试数据库...系统检查发现没有问题(0 静音)。 EEEE 错误:webapp.investor (unittest.loader._FailedTest) ImportError:无法导入测试模块:webapp.investor Traceback(最近一次调用最后一次):ModuleNotFoundError:没有名为“webapp.investor”的模块`
  • 你确定你使用的是和pycharm一样的虚拟环境吗?

标签: python django python-3.x


【解决方案1】:

您正在尝试使用from unittest import TestCase 导入 Python 的测试用例。您需要导入 Django 的。替换为

from django.test import TestCase

【讨论】:

  • 对不起,我之前没有提到这个,我也试过了。这只会给我一个稍微不同的错误:' django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES,但未配置设置。您必须在访问设置之前定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure()。 ' 另外,这一次我什至不需要从应用程序中导入任何东西,它会因单独使用 ` from django.test import TestCase ` 而失败
  • 我认为您的项目结构有问题。能不能在你的manage.py所在目录输入dir,然后分享结果?
  • 修改原问题:i.stack.imgur.com/pr1w1.png
  • 你可以试试:python manage.py test investor.tests --settings=webapp.settings
  • 好消息,错误消失了:为别名“默认”创建测试数据库...系统检查发现没有问题(0 静音)。 . -------------------------------------------------- -------------------- Ran 1 test in 0.000s OK Destroying test database for alias 'default'... 但是,为了快速测试,我添加了另一个测试,但它仍然说只运行了一个测试。
猜你喜欢
  • 2016-05-20
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 2017-01-01
  • 1970-01-01
  • 2012-07-14
相关资源
最近更新 更多