Solving Django Error – No module named ‘django.core.urlresolvers’

No module named ‘django.core.urlresolvers’ is an error which occurs because of using deprecated django modules.

This post is my attempt to explain to you why this error occurs and how you can solve it, I will also include multiple solutions that could be considered as alternative fixes to the error.

Explaining the Error : Django Error – No module named ‘django.core.urlresolvers’

First, we need to reproduce the error. In order to that you can use the code bellow.

                                                                       #
from django.core.urlresolvers import reverse 
                                                                       #

Let us start by checking out the message of the error. Bellow is the error message.

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

Bellow, you can find the solution which have worked for me plus other popular possible solutions among developers who already faced this error.

Solution 1 : use django.urls.

The first solution is simple, change all imports to use django.urls instead of django.core.urlresolvers.

Just Like in the example bellow.

                                                                       #
from django.urls import reverse
                                                                       #

It is worth to note that the switch from django.core.urlresolvers to django.urls caused a lot of features to be deprecated/removed, this means you need to make changes to your code so it can serve the same purpose as it did before.

Never forget to replace the code bellow with the line of code after it.

                                                                       #
from django.core.urlresolvers import reverse # old
                                                                       #
                                                                       #
from django.urls import reverse # new
                                                                       #

The solution should work, if it does not work then try the solution bellow.

Solution 2 : for Travis Build users

The second solution is for Travis Build users, in the latest versions of python where you encounter this error, the only fix is to try the command bellow. It works for most developers.

You can try the commands bellow as a quick and easy fix.

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

This solution is very efficient. Try using the solution bellow if the solution above did not work for you.

Solution 3 : update Django.

We already explained that urlresolver has been removed in later versions of Django.

So, the simplest and last solution is to upgrade your version of Django, the following command will do the trick.

                                                                       #
pip install django==4.1.1 --upgrade
                                                                       #

I truly believe that the solutions presented above should solve the problem for most developers.

This should be enough to get rid of the error for good. Consider donating to our Kofi account using the red button on top of this page.

Summing-up

This is the end of our article, I hope you found our article and website useful, never give up, keep creating and keep coding. Errors are normal in our field, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/