Django/Python "django.core.exceptions.ImproperlyConfigured: Cannot import 'contact'. Check that '...apps.contact.apps.ContactConfig.name' is correct" Django/Python“django.core.exceptions.ImproperlyConfigured:无法导入'contact'。请检查'...apps.contact.apps.ContactConfig.name'是否正确。”

8

希望大家能够帮我一把...

我的工作流程:

|.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", ]

的毕设项目网站/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 中:

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

我的意思是我不确定发生了什么事情,是目录问题、名称问题还是在执行make compose-start之前缺少某些步骤。如果你能帮助我解决这个问题,并解释我做错了什么,我将非常感激!谢谢。


你尝试过创建迁移并进行迁移吗? - fakeMake
是的,我仍然会得到某种形式的ImportError。 raise ImportError(msg) ImportError: 模块 'capstone_project_website.apps' 不包含 'ContactConfig' 类。 错误:1 make: *** [compose-manage-py] 错误1 - RDeaver
3个回答

16

尝试将这个改成:

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

替换原来的:

class ContactConfig(AppConfig): name = "contact"


1
这是做什么的?我遇到了类似的问题,在名称中添加“apps。”似乎可以在PyCharm中运行测试,但它会在其他构建/组合中失败,并显示以下错误:django.core.exceptions.ImproperlyConfigured: Cannot import 'app.splitter'. Check that 'splitter.apps.SplitterConfig.name' is correct. (splitter是应用程序的名称) - AlphaCR

2

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

django==3.1.8


当我执行 pip freeze 命令时,它显示我正在使用:Django==3.2 - RDeaver
请切换到 django==3.1.8 并告诉我错误信息。他们已经更改了 app_config。很可能是因为这个原因,你才会收到错误提示。 - Musfiqur Rahman
刚刚运行了一下,但还是提示找不到我的应用程序。ImportError: Module 'capstone_project_website.apps' does not contain a 'ContactConfig' class. - RDeaver

0
这是由于python version引起的问题,所以我在使用Python版本3.7.x时也遇到了类似的问题。但当我切换到3.9.x时,问题得到了解决。
这就是我试图做的事情:
(base) Imrans-MacBook-Pro:django-vue imran$ source ./venv/bin/activate
(venv) (base) Imrans-MacBook-Pro:django-vue imran$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/imran/Workspace/django-vue/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()

然后我使用3.9 ssh进入docker设置,以下是结果:

(venv) (base) Imrans-MacBook-Pro:django-vue imran$ docker-compose exec backend sh
# python --version
Python 3.9.15
# python manage.py makemigrations
Migrations for 'core':
  core/migrations/0003_product.py
    - Create model Product

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接