Django No module named 'django.db.migrations.migration'

tvz2xvvm  于 2023-05-23  发布在  Go
关注(0)|答案(3)|浏览(180)

我的项目中有一个自定义用户模型。我想通过删除数据库中的所有表并删除迁移来删除它并返回默认用户模型。在此之后,我试图运行python manage.py makemigrations命令,但它写道:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
  File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
    app_config = AppConfig.create(entry)
  File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
    mod = import_module(mod_path)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/contrib/contenttypes/apps.py", line 9, in <module>
    from .management import (
  File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/contrib/contenttypes/management/__init__.py", line 2, in <module>
    from django.db import DEFAULT_DB_ALIAS, migrations, router, transaction
  File "/home/irakliy01/Projects/Project/PythonVEnv/lib/python3.6/site-packages/django/db/migrations/__init__.py", line 1, in <module>
    from .migration import Migration, swappable_dependency  # NOQA
ModuleNotFoundError: No module named 'django.db.migrations.migration'

我不知道我做错了什么。

ltskdhd1

ltskdhd11#

您的Django安装似乎已损坏。您可以使用以下命令重新安装:

pip3 uninstall Django
pip3 install Django

通常情况下,如果你在常规操作系统的python安装中安装了Django,然后安装了一个virtualenv副本并试图运行django服务器,就会发生这种情况。如果Django已经存在,你需要先删除它,然后在你的virtualenv中重新安装它。

c86crjj0

c86crjj02#

假设您在虚拟环境中运行,您可能实际上也删除了/django/db/migrations/下的文件。这里的错误是显而易见的。您需要重新安装django来获取文件以成功迁移。
为此,请检查Django版本

python -m django --version

然后使用以下命令安装相同的版本

pip install --upgrade --force-reinstall  Django==3.2.2
qxsslcnc

qxsslcnc3#

我不假思索地复制粘贴并在根目录下运行了这个oneliner。

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete

好吧,我在venv中安装了Django,并且-令人惊讶的是-在Django源代码中有一个migrations包。重新安装后,一切正常。

相关问题