14

I'm upgrading my Django App from Django 1.5.5 tot 1.9, Django-cms from 2.4.3 to 3.3 (and all corresponding packages).

After I've plowed through all the errors of depreciated functions I now stumble on an error that I cannot understand: 'No module named migration'

I get this error when running (in a virtualenv): - python manage.py runserver and also when I run - python manage.py migrate

Traceback (most recent call last):
  File "manage.py", line 20, in <module>
    execute_from_command_line(sys.argv)
  File "/var/www/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/var/www/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/www/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 195, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/var/www/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 39, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/var/www/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 16, in <module>
    from django.db.migrations.autodetector import MigrationAutodetector
  File "/var/www/env/local/lib/python2.7/site-packages/django/db/migrations/__init__.py", line 1, in <module>
    from .migration import Migration, swappable_dependency  # NOQA
ImportError: No module named migration

manage.py

#!/usr/bin/env python2
import os
import sys

if __name__ == "__main__":

    settings_module_path = 'ais.settings.production'
    ########## Attempt to override settings using local settings
    try:
        from ais.settings.local_settings import *
        # For developmentent, file will probably hold the following:
        settings_module_path = 'ais.settings.development'
        print "!!!manage.py settings overwritten!!!"
    except ImportError:
        pass
    os.environ['DJANGO_SETTINGS_MODULE'] = settings_module_path

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

migrate.sh

#!/bin/sh
echo "Starting ..."

echo ">> Deleting old migrations"
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete


# Optional
echo ">> Deleting sqlite  (if exists) database"
find . -name "db.sqlite3" -delete

echo ">> Running manage.py makemigrations"
python manage.py makemigrations

echo ">> Running manage.py migrate"
python manage.py migrate

echo ">> Done"
A. Nurb
  • 496
  • 1
  • 3
  • 17
  • 2
    Would you try uninstalling and then reinstalling Django in your virtualenv? `pip uninstall django && pip install django==1.11` (or whichever version you're upgrading to). – YPCrumble Apr 20 '17 at 19:15
  • I did it .... but still the same error – A. Nurb Apr 20 '17 at 19:36
  • Make sure you're using the correct version of Django. Type `python -c "import django; print(django.get_version())"` into the console. What does that produce? Then check your version of Python with the virtualenvironment activated (`python --version`). What's the version? Last thing, is that when you upgrade Django you probably didn't also upgrade `manage.py` - make sure that `#!/usr/bin/env python` is the first line in that file. – YPCrumble Apr 20 '17 at 19:59
  • Please also post your entire `manage.py` file if none of these worked. – YPCrumble Apr 20 '17 at 19:59
  • @YPCrumble I found it. I used a script to reset the migrations. See bottom original question for migrate.sh. And it seems that that script is deleting more than just the migration files. – A. Nurb Apr 21 '17 at 05:16
  • Make sure your virtual environment directory do not reside along with `migrate.sh` script path. Possible way is move all django apps to 'src' directory and `migrate.sh` should run only on 'src' directory contents. – Ibrahim Jun 14 '17 at 19:46

7 Answers7

30

If your error still like :

 from .migration import Migration, swappable_dependency  # NOQA
ImportError: No module named 'django.db.migrations.migration'

You need to reinstall dajngo

Check You Django version and then Force Reinstall it

python -m django --version

pip install --upgrade --force-reinstall package

  pip install --upgrade --force-reinstall  Django==2.0.5
Tarun Sharma
  • 462
  • 6
  • 18
20

Your script appears to be the problem. It is trying to delete your migrations, but it's actually also deleting the contents of Django's /django/db/migrations/ file as well. Note that it explicitly doesn't delete the __init__.py file but it does delete the others.

One option is just to remove these lines:

echo ">> Deleting old migrations" 
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete 
find . -path "*/migrations/*.pyc"  -delete

You shouldn't be deleting old migrations anyway once you're running Django on production because you might want to add custom code to a migration. This looks like a convenience script for development.

YPCrumble
  • 26,610
  • 23
  • 107
  • 172
  • 1
    ahah, nice one, got the same issue with a virtualenv within my repository :D – Simon Ninon Feb 19 '20 at 00:26
  • This will also remove the migrations inside core Django module if you are using virtualenv. This will break the project apart and would need to reinstall Django again to fix it. Don`t use wildcard to remove old migrations – Danwand N S Feb 27 '22 at 02:14
5

As @YPCrumble pointed out, your ">> Deleting old migrations" script deleted /django/db/migrations/ file as well. To restore it back, you need to uninstall Django and reinstall it.

raj
  • 187
  • 1
  • 11
3

Just reinstall the django version or upgrade the version. This solves my problem.

pip install --upgrade django==1.11.18

Then makemigrations

Hariharan AR
  • 1,386
  • 11
  • 20
2

You must have deleted the migrations file

In that case try force reinstalling Django:

pip install --upgrade --force-reinstall  Django
PSN
  • 2,326
  • 3
  • 27
  • 52
1

Django models has a manage attribute.It can manage database table create or not create.manage is false not create database table, manage is true create tabe.

Class User(models.Model):
    username = models.CharField(max_length=255)

    Class Meta:
        manage = False
        manage = True
  • Drop database and create new database.
  • Update settings file (database connection).
  • Delete exist migrations files.
  • Create new migrations.
  • Run new migrations.
waruna k
  • 842
  • 10
  • 20
0

Try to place your virtualenv repository outside of your project. Or run the script in a path with it does not include your virtualenv repository.

Or you can exclude virtualenv repository from your find path. Check How to exclude a directory in find . command

kayuapi_my
  • 498
  • 1
  • 6
  • 9