Python Django “Module not found” error.

In this Python Django tutorial, we are going to discuss the most common error i.e. Python Django “Module not found” error we face this error while building up our Python Django project. There are several reasons why we get this error while running our Django server.

Django Module Not Found Error
Django Module Not Found Error

In further steps, we will discuss the reasons for this error and we will also see how to fix this error.

Reasons for error and how to fix it?

In the further steps, we will come to know the reasons for this error and how to fix it. So let’s discuss all the potential reasons for this error.

1. Not registering the module or app in the settings.py file.

For better understanding let’s take an example of a basic structure of the settings.py file which is present in the Project directory.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'rest_framework', ]

Solution:

As we can see there is an application defined in the INSTALLED_APPS section named “myapp“.

If you have created an application then it is necessary to register it in the INSTALLED_APPS section of settings.py file and also make sure you have correctly spelled the app name or the module name.

Whenever we use any external packages and libraries like numpy, pandas, or rest_framework in that case also the package name is supposed to be registered in the settings.py file as shown in the above code.

2. Pip installation of external packages.

When we build a Python Django project we require to import packages. Python consists of many inbuilt modules like:

  • os module.
  • random module.
  • math module.
  • time module.

but many times we require external modules like:

  • rest_framework
  • phone_field
  • language_field

Sometimes we use these external modules in the same way we use the in-built modules for example we need to define the phone number field in our models.py file so we import that field as shown below code.

from django.db import models
import datetime


from phonenumber_field.modelfields import PhoneNumberField

but after this at the time of execution, it will throw an error in Python Django “No module named PhoneNumberField” because it is an external module and we need to install it first in our environment using the following command in the terminal.

pip install phonenumberfield

Now after installing this module with the help of pip, we will not get the Python Django ModuleNotFoundError: No module named ‘phonenumberfield’ error since we have installed it and it is there in our environment.

Read Error: Django Python setup.py egg_info failed

3. Pip not installing packages at the correct path.

When we move a project from one folder to another folder or a drive, then also we face this issue when we do not change the project path in a correct manner.

In this case, if you copied a project from a folder to a drive then it might happen that when you are installing any external module the pip package manager is installing that module in the previous path.

The correct method to transfer a project file is explained in the following postulates.

  • Deactivate the virtual environment if it’s currently active by running the command deactivate.
  • Copy the entire virtual environment folder to the new location using Windows Explorer or the robocopy command in the Command Prompt.
  • To use robocopy replace the path C:\path\to\old\venv with the path to the original virtual environment folder, and D:\path\to\new\venv with the path to the new location.
  • Open the activate.bat script in a text editor such as Notepad.
  • Find the lines that set the VIRTUAL_ENV PATH environment variables.
  • Update the paths to match the new location of the virtual environment.
  • Activate the environment using activate.bat follow the code D:\path\to\new\venv\Scripts\activate.bat
  • In the end upgrade pip using the command pip install –upgrade.

Conclusion

In this Python Django tutorial, we have learned how to fix the Django “module not found” error and we have discussed all the possible ways of its occurrence later we learned how to fix that error in different ways.

We have also learned how to transfer a Python Django Project from one location to another location.

You may also like to read the following articles: