irakliy01
irakliy01

Reputation: 385

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

I had a custom user model in my project. I wanted to delete it and return default user model by deleting all tables in my database and deleting migrations. After this, I tried to run python manage.py makemigrations command but it writes:

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'

I have no idea what I did wrong.

Upvotes: 26

Views: 26205

Answers (5)

pfrud
pfrud

Reputation: 21

I had the same problem. Tried to reinstall django and my whole .venv but I still got the same error.

What fixed it for me was to run manage.py makemigrations myapp --empty to apply an initial migration and after that makemigrations and migrate.

Upvotes: 0

KhaledMohamedP
KhaledMohamedP

Reputation: 5532

In this case, its best to reset your virtual environment and reinstall dependencies, you can follow these steps or follow the project :

python -m venv .venv
source .venv/bin/activate  # or source .venv/bin/activate.fish
pip install -r requirements.txt  # or poetry install

Upvotes: 1

Tom Wojcik
Tom Wojcik

Reputation: 6179

Without thinking I copy-pasted and run this oneliner in the root directory.

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

Well, I had Django installed within the venv, and - surprise surprise - there's a migrations package in the Django source code. After reinstall everything works fine.

Upvotes: 8

s4n7h0
s4n7h0

Reputation: 183

Assuming you are running in the virtual environment, you might have actually deleted the files under /django/db/migrations/ as well. So the error is obvious here. You would need to reinstall django to get the files back for successful migration.

To do this, check the django version

python -m django --version

Then install the same version using following command

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

Upvotes: 14

Joel G Mathew
Joel G Mathew

Reputation: 8061

Your Django installation seems corrupted. You can reinstall with:

pip3 uninstall Django
pip3 install Django

Usually this happens if you installed Django in your regular operating system's python installation and then later installed a virtualenv copy and tried to run the django server. You need to reinstall Django in your virtualenv, after removing it first if it already exists.

Upvotes: 44

Related Questions