'ImportError: No module named 'django.core.urlresolvers'

I am working on Django project where I need to create a form for inputs. I tried to import reverse from django.core.urlresolvers. I got an error:

line 2, in from django.core.urlresolvers import reverse ImportError: No module named 'django.core.urlresolvers'

I am using Python 3.5.2, Django 2.0 and MySQL.



Solution 1:[1]

if you want to import reverse, import it from django.urls

from django.urls import reverse

Solution 2:[2]

You need replace all occurrences of:

from django.core.urlresolvers import reverse

to:

from django.urls import reverse

enter image description here

NOTE: The same apply to reverse_lazy

in Pycharm Cmd+Shift+R for starting replacment in Path.

Solution 3:[3]

use from django.urls import reverse instead of from django.core.urlresolvers import reverse

Solution 4:[4]

For those who might be trying to create a Travis Build, the default path from which Django is installed from the requirements.txt file points to a repo whose django_extensions module has not been updated. The only workaround, for now, is to install from the master branch using pip. That is where the patch is made. But for now, we'll have to wait.

You can try this in the meantime, it might help

- pip install git+https://github.com/chibisov/drf-extensions.git@master

- pip install git+https://github.com/django-extensions/django-extensions.git@master

Solution 5:[5]

For django version greater than 2.0 use:

from django.urls import reverse

in your models.py file.

Solution 6:[6]

urlresolver has been removed in the higher version of Django - Please upgrade your django installation. I fixed it using the following command.

pip install django==2.0 --upgrade

Solution 7:[7]

If your builds on TravisCI are failing for this particular reason, you can resolve the issue by updating the Django Extensions in your requirements.txt

pip install --upgrade django-extensions

This will update the extensions to use Django 2+ modules.

Solution 8:[8]

To solve this either you down-grade the Django to any version lesser than 2.0. pip install Django==1.11.29.

Solution 9:[9]

Upgrading Django 1.9 (Python 2.7) to Django 3.2 (Python 3.9)

This could be solved in a one line bash replacement:

grep -ril "from django.core.urlresolvers" your_source_code_folder | xargs sed -i 's@from django.core.urlresolvers@from django.urls@g'

Solution 10:[10]

In my case the problem was that I had outdated django-stronghold installed (0.2.9). And even though in the code I had:

from django.urls import reverse

I still encountered the error. After I upgraded the version to django-stronghold==0.4.0 the problem disappeard.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Surajano
Solution 2
Solution 3 Amit Bahadur
Solution 4 emalinga
Solution 5 vgel
Solution 6 Bapuray
Solution 7 willieswanjala
Solution 8 Alex
Solution 9 shakaran
Solution 10 gawi