ModuleNotFoundError: 在运行Django 4.1中的测试时找不到模块 ‘django.config’。

huangapple go评论67阅读模式
英文:

ModuleNotFoundError: No module named 'django.config' when running test in Django 4.1

问题

我有一个django(v4.1)项目,没有定义任何测试。在我开始编写测试之前,我运行了python manage.py test,期望得到类似于以下的结果:

找到 0 个测试(s)。
系统检查未发现问题(0 个被忽略)。
.....
----------------------------------------------------------------------
在 0.000 秒内运行了 0 个测试

OK

但是我得到了以下结果:

找到 1 个测试(s)。
系统检查未发现问题(0 个被忽略)。
E
======================================================================
错误: django.config (unittest.loader._FailedTest.django.config)
----------------------------------------------------------------------
ImportError: 无法导入测试模块:django.config
Traceback(最近的调用最后):
  File "/usr/local/lib/python3.11/unittest/loader.py", line 440, in _find_test_path
    package = self._get_module_from_name(name)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/unittest/loader.py", line 350, in _get_module_from_name
    __import__(name)
ModuleNotFoundError: 找不到名为 'django.config' 的模块。


----------------------------------------------------------------------
在 0.000 秒内运行了 1 个测试

FAILED(错误数=1)

我已经检查了这个问题,但似乎更多涉及从Django 2迁移到3时的迁移问题以及如何运行测试。

澄清

  1. 当我说没有定义测试时,我的意思是自动生成的manage.py startapp在三个应用程序中创建了test.py文件,但我已经注释掉了标准的导入语句 # from django.test import TestCase

  2. 我尝试使用grep -r "django.config" .从存储库目录中(位于django的BASE_DIRECTORY上方)搜索了任何django.config的实例,但在任何文件中都找不到它。

  3. 我将“项目”目录(包含settings)命名为“config”,所以我想知道是否会出现一些与导入有关的混淆。执行了所有“config”出现的搜索:

$ grep -r "config" .
./templates/base.html:    使用设置中的“builtins”键(在./config/base/templates.py中)。
./config/admin.py:    default_site = 'config.admin.CustomAdminSite'
./config/asgi.py:服务器项目的ASGI配置。
./config/settings/base/databases.py:from config.utilities import dprint
./config/settings/base/databases.py:        "NAME": str(BASE_DIR / "config/db.sqlite3"),
./config/settings/base/core.py:ROOT_URLCONF = "config.urls"
./config/settings/base/core.py:WSGI_APPLICATION = "config.wsgi.application"
./config/settings/base/authentication.py:# authall配置设置
./config/settings/base/applications.py:    # 请参见./authentication.py,了解有关所有auth的社交账户配置
./config/settings/base/applications.py:    "config.admin.CustomAdminConfig", # 替代django.contrib.admin
./config/settings/__init__.py:from config.utilities import dprint
./config/settings/__init__.py:SECRETS_DIR = BASE_DIR / 'config/secrets/'
./config/wsgi.py:服务器项目的WSGI配置。
./apps/accounts/admin.py:from config.utilities import dprint
./apps/core/views.py:from config.utilities import dprint
./manage.py:    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

在django的BASE_DIRECTORY下方的项目结构:

├── __init__.py
├── apps
│   ├── accounts
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── forms.py
│   │   ├── managers.py
│   │   ├── models.py
│   │   ├── signals.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── core
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   ├── urls.py
│   │   └── views.py
│   └── recipes
│       ├── __init__.py
│       ├── admin.py
│       ├── apps.py
│       ├── models.py
│       ├── tests.py
│       ├── urls.py
│       └── views.py
├── config
│   ├── __init__.py
│   ├── admin.py
│   ├── asgi.py
│   ├── db.sqlite3
│   ├── secrets
│   │   ├── development.secret
│   │   └── secret.template
│   ├── settings
│   │   ├── __init__.py
│   │   ├── base
│   │   └── development
│   ├── tests.py
│   ├── urls.py
│   ├── utilities.py
│   └── wsgi.py
├── manage.py
├── static
│   ├── css
│   │   └── styles.css
│   └── js
└── templates
    ├── allauth
    │   └── account
    ├── base.html
    ├── base_with_nav.html
    ├── core
    │   ├── index.html
    │   └── item1.html
    ├── navbar.html
    └── navbar_rhs_items.html
英文:

I have a django (v4.1) project with no tests defined. Before I started writing tests, I ran python manage.py test, expecting to get something like:

Found 0 test(s).
System check identified no issues (0 silenced).
....
----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

But instead I got this:

Found 1 test(s).
System check identified no issues (0 silenced).
E
======================================================================
ERROR: django.config (unittest.loader._FailedTest.django.config)
----------------------------------------------------------------------
ImportError: Failed to import test module: django.config
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/unittest/loader.py", line 440, in _find_test_path
    package = self._get_module_from_name(name)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/unittest/loader.py", line 350, in _get_module_from_name
    __import__(name)
ModuleNotFoundError: No module named 'django.config'


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

I've checked this question but it seems more to do with a migration problem from Django 2 to 3 and how to run tests.

Clarifications:

  1. When I say there are no tests defined, I mean that there are the auto-generated test.py files created by manage.py startapp in three applications, but I've commented out the standard import statement # from django.test import TestCase.

  2. I tried searching for any instances of django.config using grep -r "django.config" . from the repo directory (which is above django's BASE_DIRECTORY and there are no instances of that in any of the files.

  3. I have named the "project" directory (the one containing settings) "config" so wondered if there was come import confusion stemming from that. Did a search for all occurrences of "config":

$ grep -r "config" .
./templates/base.html:    using the 'builtins' key of OPTIONS in settings (in ./config/base/templates.py).
./config/admin.py:    default_site = 'config.admin.CustomAdminSite'
./config/asgi.py:ASGI config for server project.
./config/settings/base/databases.py:from config.utilities import dprint
./config/settings/base/databases.py:        "NAME": str(BASE_DIR / "config/db.sqlite3"),
./config/settings/base/core.py:ROOT_URLCONF = "config.urls"
./config/settings/base/core.py:WSGI_APPLICATION = "config.wsgi.application"
./config/settings/base/authentication.py:# authall configuration settings
./config/settings/base/applications.py:    # NB see ./authentication.py for social account config of allauth
./config/settings/base/applications.py:    "config.admin.CustomAdminConfig", # replaces django.contrib.admin
./config/settings/__init__.py:from config.utilities import dprint
./config/settings/__init__.py:SECRETS_DIR = BASE_DIR / 'config/secrets/'
./config/wsgi.py:WSGI config for server project.
./apps/accounts/admin.py:from config.utilities import dprint
./apps/core/views.py:from config.utilities import dprint
./manage.py:    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

Project structure beneath django's BASE_DIRECTORY:

├── __init__.py
├── apps
│   ├── accounts
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── forms.py
│   │   ├── managers.py
│   │   ├── models.py
│   │   ├── signals.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── core
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   ├── urls.py
│   │   └── views.py
│   └── recipes
│       ├── __init__.py
│       ├── admin.py
│       ├── apps.py
│       ├── models.py
│       ├── tests.py
│       ├── urls.py
│       └── views.py
├── config
│   ├── __init__.py
│   ├── admin.py
│   ├── asgi.py
│   ├── db.sqlite3
│   ├── secrets
│   │   ├── development.secret
│   │   └── secret.template
│   ├── settings
│   │   ├── __init__.py
│   │   ├── base
│   │   └── development
│   ├── tests.py
│   ├── urls.py
│   ├── utilities.py
│   └── wsgi.py
├── manage.py
├── static
│   ├── css
│   │   └── styles.css
│   └── js
└── templates
    ├── allauth
    │   └── account
    ├── base.html
    ├── base_with_nav.html
    ├── core
    │   ├── index.html
    │   └── item1.html
    ├── navbar.html
    └── navbar_rhs_items.html

答案1

得分: 0

问题是,正如John Gordon所指出的,我已经将BASE_DIRECTORY命名为django。 重命名它(我怀疑可以是任何其他名称,但在我的情况下我使用了src)解决了这个问题。

如果John想为此提供答案,我将把你的答案标记为已接受答案。 通过这个答案来结束这个问题。

英文:

The problem was that -- as pointed out by John Gordon -- I had named the BASE_DIRECTORY django. Renaming it (I suspect to anything else, but in my case I used src) solved the problem.

John if you want to provide an answer for this I'll mark yours as the accepted answer. Just closing off the question with this one.

huangapple
  • 本文由 发表于 2023年2月18日 08:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定