ImportError: No module named 'django.core.urlresolvers'
Asked Answered
S

12

251

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.

Scraggly answered 31/3, 2017 at 11:2 Comment(1)
looks like django is not installed on your PATH.Pagepageant
I
499

Django 2.0 removes the django.core.urlresolvers module, which was moved to django.urls in version 1.10. You should change any import to use django.urls instead, like this:

from django.urls import reverse

Note that Django 2.0 removes some features that previously were in django.core.urlresolvers, so you might have to make some more changes before your code works. See the features deprecated in 1.9 for details on those additional changes.

Impost answered 31/3, 2017 at 11:20 Comment(2)
I have this error in a third party module: how can I solve? I could edit the module but what then when I upgrade a new uncorrected version? Is it possible to override?Yorgen
You could update and send a pull request to the third party moduleTrivet
J
68

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

from django.urls import reverse
Jandy answered 31/3, 2017 at 11:21 Comment(0)
S
31

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.

Spendthrift answered 5/3, 2018 at 14:50 Comment(1)
I got same error, tried this and now get error File "C:\Users\displ\Code\.virtualenvs\dev\lib\site-packages\cms\utils\i18n.py", line 4, in <module> from django.core.urlresolvers import get_resolver, LocaleRegexURLResolver ModuleNotFoundError: No module named 'django.core.urlresolvers', had to do what correct answer said replace all references. Django kind of sucks though, lot of stuff doesn't work from get go.Tecumseh
R
8

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

Reform answered 19/11, 2020 at 14:42 Comment(0)
A
3

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

Antipole answered 24/8, 2018 at 4:34 Comment(0)
J
3

For django version greater than 2.0 use:

from django.urls import reverse

in your models.py file.

Jessiejessika answered 27/10, 2019 at 6:11 Comment(0)
F
2

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
Friel answered 17/5, 2019 at 12:3 Comment(0)
C
2

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'
Cinematograph answered 26/6, 2021 at 16:50 Comment(0)
H
1

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.

Hamburg answered 29/3, 2019 at 3:2 Comment(0)
F
1

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

Flintshire answered 16/3, 2020 at 11:52 Comment(0)
M
0

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.

Monkeypot answered 23/10, 2020 at 8:28 Comment(0)
A
0

I had the same error, but that was just because of my url.py file. I fixed it like below:

from drf_spectacular.views import (
SpectacularAPIView,
SpectacularSwaggerView,
)
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/schema/', SpectacularAPIView.as_view(), name='api-schema',),
    path('api/docs/',  SpectacularSwaggerView.as_view(url_name='api-schema'), name='api-docs',),
]
Azarria answered 10/2, 2023 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.