Django/Python“django.core.exceptions.ImproperlyConfigured:无法导入‘联系人’。检查‘...apps.contact.apps.ContactC

Posted

技术标签:

【中文标题】Django/Python“django.core.exceptions.ImproperlyConfigured:无法导入‘联系人’。检查‘...apps.contact.apps.ContactConfig.name’是不是正确”【英文标题】:Django/Python "django.core.exceptions.ImproperlyConfigured: Cannot import 'contact'. Check that '...apps.contact.apps.ContactConfig.name' is correct"Django/Python“django.core.exceptions.ImproperlyConfigured:无法导入‘联系人’。检查‘...apps.contact.apps.ContactConfig.name’是否正确” 【发布时间】:2021-07-25 05:39:35 【问题描述】:

希望你们都可以帮我解决这个问题...

我的工作流程:

|.vscode:
|capstone_project_website:
|   -_pycache_:
|   -apps:
|       -_pycache_
|       -accounts:
|       -contact:    # app that is throwing errors
|           -_pycache_:
|          -migrations:
|          -_init_.py
|          -admin.py
|          -apps.py
|          -forms.py
|          -models.py
|          -test.py
|          -urls.py
|          -views.py 
|       -public:
|       -_init_.py
|   -templates: # all my .html 
|   -_init_.py
|   -asgi.py
|   -settings.py
|   -urls.py
|   -views.py
|   -wsgi.py
|requirements:
|scripts:
|static:
|.gitignore
|.python-version
|db-sqlite3
|docker-compose.yml
|Dockerfile
|Makefile #command I am running
|manage.py
|setup.cfg

我在capstone_project_website/settings.py中安装的应用程序:

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"capstone_project_website.apps.accounts",
"capstone_project_website.apps.contact", ]

我的capstone_project_website/apps/contact/apps.py:

from django.apps import AppConfig

class ContactConfig(AppConfig):
    name = "contact"

我正在运行的命令在我的Makefile:

compose-start:
    docker-compose up --remove-orphans $(options)

当我运行 make compose-start 时,我从终端收到这条消息:

make compose-start
docker-compose up --remove-orphans
Docker Compose is now in the Docker CLI, try `docker compose up`

Starting django-website_postgres_1 ... done
Starting django-website_db_migrate_1 ... done
Starting django-website_website_1    ... done

Attaching to django-website_postgres_1, django-website_db_migrate_1, django-website_website_1
db_migrate_1  | Traceback (most recent call last):
db_migrate_1  |   File "/usr/local/lib/python3.7/site-packages/django/apps/config.py", line 244, in create
db_migrate_1  |     app_module = import_module(app_name)
db_migrate_1  |   File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
db_migrate_1  |     return _bootstrap._gcd_import(name[level:], package, level)
db_migrate_1  |   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
db_migrate_1  |   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
db_migrate_1  |   File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
db_migrate_1  | ModuleNotFoundError: No module named 'contact'
db_migrate_1  |
db_migrate_1  | During handling of the above exception, another exception occurred:
db_migrate_1  |
db_migrate_1  | Traceback (most recent call last):
db_migrate_1  |   File "manage.py", line 22, in <module>
db_migrate_1  |     main()
db_migrate_1  |   File "manage.py", line 18, in main
db_migrate_1  |     execute_from_command_line(sys.argv)
db_migrate_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
db_migrate_1  |     utility.execute()
db_migrate_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
db_migrate_1  |     django.setup()
db_migrate_1  |   File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
db_migrate_1  |     apps.populate(settings.INSTALLED_APPS)
db_migrate_1  |   File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
db_migrate_1  |     app_config = AppConfig.create(entry)
db_migrate_1  |   File "/usr/local/lib/python3.7/site-packages/django/apps/config.py", line 250, in create
db_migrate_1  |     app_config_class.__qualname__,
db_migrate_1  | django.core.exceptions.ImproperlyConfigured: Cannot import 'contact'. Check that 'capstone_project_website.apps.contact.apps.ContactConfig.name' is correct.
postgres_1    |
postgres_1    | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1    |
postgres_1    | 2021-05-02 14:17:44.105 UTC [1] LOG:  starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1    | 2021-05-02 14:17:44.106 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1    | 2021-05-02 14:17:44.106 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1    | 2021-05-02 14:17:44.113 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1    | 2021-05-02 14:17:44.121 UTC [28] LOG:  database system was shut down at 2021-05-02 14:17:17 UTC
django-website_db_migrate_1 exited with code 1
postgres_1    | 2021-05-02 14:17:44.129 UTC [1] LOG:  database system is ready to accept connections
website_1     | Watching for file changes with StatReloader
^CGracefully stopping... (press Ctrl+C again to force)
Stopping django-website_website_1    ... done
Stopping django-website_postgres_1   ... done

我知道我投入了很多工作流程,但我想知道我是否设置不正确。在我的帐户应用程序上尝试makemigrations 之前,我遇到了这个问题。这个问题的答案似乎奏效了。 答案是将capstone_project_website/apps/accounts/apps.py 中的name 更改为

class AccountsConfig(AppConfig):
    name = "capstone_project_website.apps.accounts"

在已安装的应用程序中,您可以看到路径是:

"capstone_project_website.apps.accounts",

我已尝试更改...的名称...capstone_project_website/apps/contact/apps.py

class ContactConfig(AppConfig):
    name = "contact"

...到name='capstone_project_website.apps.contact' ...以及我安装的应用名称:

INSTALLED_APPS = [
    ...
    "capstone_project_website.apps.ContactConfig", ]

我不确定发生了什么,如果是目录问题、名称问题,或者在我 make compose-start 之前我缺少某些步骤。如果你能帮我解决这个问题,并解释我做错了什么,我将不胜感激! 谢谢

【问题讨论】:

您是否尝试过进行迁移和迁移???? 是的,我仍然收到某种形式的 ImportError。 raise ImportError(msg) ImportError: Module 'capstone_project_website.apps' does not contain a 'ContactConfig' class. ERROR: 1 make: *** [compose-manage-py] Error 1 【参考方案1】:

检查您的 django 版本。如果您将 django 版本更新到 3.2,请尝试切换到最早的版本。

django==3.1.8

【讨论】:

当我执行pip freeze 时,它说我正在使用:Django==3.2 请切换到django==3.1.8,让我知道错误。他们更改了 app_config。可能是因为这个原因,你得到了错误。 刚刚运行它,它仍然说它找不到我的应用程序。 ImportError: Module 'capstone_project_website.apps' does not contain a 'ContactConfig' class.【参考方案2】:

尝试改变这个:

类 ContactConfig(AppConfig): name = "联系人"

到这里:

类 ContactConfig(AppConfig): name = "apps.contact"

【讨论】:

这是做什么的?我遇到了类似的问题,并添加了“应用程序”。当我尝试在 PyCharm 中运行测试时,名称中的名称似乎有效,但它会使其他构建/组合失败,并出现 django.core.exceptions.ImproperlyConfigured: Cannot import 'app.splitter'. Check that 'splitter.apps.SplitterConfig.name' is correct. 错误(拆分器是应用程序的名称)

以上是关于Django/Python“django.core.exceptions.ImproperlyConfigured:无法导入‘联系人’。检查‘...apps.contact.apps.ContactC的主要内容,如果未能解决你的问题,请参考以下文章

Django 1.7 抛出 django.core.exceptions.AppRegistryNotReady:模型尚未加载

django + virtualenv + gunicorn - 没有名为 django.core.wsgi 的模块?

Django分页:使用django.core.paginator模块

错误:'没有名为'django.core.urlresolvers'的模块

Django Celery 计划任务 django.core.exceptions.ImproperlyConfigured

django 2.0a1 中没有名为 django.core.urlresovers 的模块