How to fix the “modulenotfounderror no module named ‘django’” Error

Python allows the import of various libraries and extensions which interact well with Python and allow the user to code much more efficiently. One of these libraries is the Django library which is imported to Python. This is a framework based on Python that assists in developing and maintaining websites with improved security. While attempting to run the Python code involving Django, an error named  “modulenotfounderror no module named ‘django‘” may occur.

In this post, we will provide the possible reasons that invoke the error “modulenotfounderror no module named ‘django’” and the relevant solutions as well.

Resolve the “modulenotfounderror no module named ‘django’” Problem

There exist a few reasons that will prompt this issue on your system. This section will elaborate on all these reasons and also provide possible fixes for them. The error statement has been demonstrated in the snippet below:

Note: For this article, the following Python file will be considered as the sample:

Reason 1: Django Not Installed

The most commonly occurring reason for this error is that the Django package is not installed on your system using the Python installer which is the pip installer that is used for installing packages and libraries related to Python. If the package is not installed, the library cannot be located. In such a case, the “import” statement will invoke this error.

Solution: Install Django

The first solution for this issue that crosses your mind is to install the missing Django package on your system. To achieve this, the pip keyword is used, which is commonly utilized for the installation of Python packages. Run the command below to install Django:

$ pip3 install django

Once it has been installed onto your system, you can again run your Python file and see that the error no longer persists:

Reason 2: Package not Installed in Virtual Environment

Sometimes, installing the package globally can be the cause of the error. In these scenarios, it is best to install the package in a virtual environment and then utilize it there.

Solution: Create Virtual Environment and Install Django

The solution to this problem is to create a new virtual environment on your system and install the Django package on that environment. To create and activate a python virtual environment, use the below-stated command where the first command creates the virtual environment and the second command activates it:

$ python3 -m venv venv
$ source venv/bin/activate

Inside the virtual environment, run this command to install the Django package:

$ pip3 install django

Reason 3: File Named as Django

Another common cause behind this issue is that there exists a file that is named “”. This will confuse the system compiler as it will be conflicted between the library and the file name.

Solution: Rename the File

A very simple fix to this problem is to rename the Python file. If it is named “django” then rename the file to a more appropriate name.

Reason 4: Variable Named as Django

Similar to the previous problem, this error can also be invoked when a variable inside the Python code has been named “django”. When this happens, the compiler becomes confused and prompts the error to the user.

Solution: Change Variable Name

To fix this issue, you need to go through your Python code and search for any variables named “django”. Once you locate these variables, you need to change the name of that variable to anything else that is allowed within the Python requirements of a variable name.


The “modulenotfounderror no module named ‘django’” problem is invoked when the package is not installed on your system or globally. It also occurs when there is a file or variable named “django”. This problem can be resolved by installing the Django package using pip on a virtual environment or by renaming the file or variable causing the contradiction. This post demonstrates the reason for this problem, and their respective solutions are also shown.