1. 程式人生 > 其它 >django3執行遷移出現:django.core.exceptions.ImproperlyConfigured: Cannot import

django3執行遷移出現:django.core.exceptions.ImproperlyConfigured: Cannot import

django3執行資料庫遷移時出現如下錯誤:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/django/apps/config.py", line 244, in create
    app_module = import_module(app_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File 
"<frozen importlib._bootstrap>", line 1014, in _gcd_import File "<frozen importlib._bootstrap>", line 991, in _find_and_load File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked ModuleNotFoundError: No module named 'students' During handling of the above exception, another exception occurred: 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 "/usr/local/lib/python3.8/dist-packages/django/core/management/__init__.py", line 419, in execute_from_command_line utility.execute() File "/usr/local/lib/python3.8/dist-packages/django/core/management/__init__.py
", line 395, in execute django.setup() File "/usr/local/lib/python3.8/dist-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/usr/local/lib/python3.8/dist-packages/django/apps/registry.py", line 91, in populate app_config = AppConfig.create(entry) File "/usr/local/lib/python3.8/dist-packages/django/apps/config.py", line 246, in create raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Cannot import 'students'. Check that 'apps.students.apps.studentsConfig.name' is correct.

出現原因:因為students這個被建立在apps的資料夾裡

apps.py檔案內容

from django.apps import AppConfig


class StudentsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'students'

解決辦法:將name = 'students',改成name = 'apps.students'

from django.apps import AppConfig


class StudentsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'apps.students'

然後再執行,python3 manage.py makemigrates