Python, Django

How to Fix “ModuleNotFoundError: No Module Named Django” in Python

ModuleNotFoundError No Module Named Django

You finally decided to learn Django. You followed a tutorial, wrote your first lines of code, ran the command with excitement and then Python shut you down with this message.

ModuleNotFoundError: No module named ‘django’

This error is one of the most common problems beginners face when starting with Django. The good news is that it’s not scary, it’s not rare, and it’s almost always easy to fix once you understand what’s really going on.

What Does “ModuleNotFoundError: No Module Named Django”

Before fixing the error, let’s understand it.

Python is telling you something very simple:

Python looked for Django.
Python did not find Django.
So Python gave up and complained.

That’s it.

This error does not mean Django is broken.
It does not mean your code is bad.
It usually means Django is not installed or installed in the wrong place.

Once you understand that, fixing it becomes much easier.

The Most Common Reason: Django Is Not Installed

Let’s start with the most obvious cause.

Many people assume Django comes with Python. It doesn’t.

Python does not include Django by default. You must install it yourself.

How to Check If Django Is Installed:

Open your terminal or command prompt and run:

python -m django --version

If Django is installed, you’ll see a version number like:

4.2.5

If you see the same error again, Django is not installed in that Python environment.

How to Install Django the Correct Way

Now let’s fix it properly.

Installing Django Using pip:

Run this command:

pip install django

If you’re using Python 3 and that doesn’t work, try:

pip3 install django

After installation finishes, check again:

python -m django --version

If you see a version number, Django is now installed.

But wait. If it were always this easy, this article wouldn’t need to be 1000 words long.

Let’s move on to the more sneaky problems.

Python Version Mismatch The Silent Trouble Maker

This is where many people get stuck.

You might have multiple versions of Python installed on your system. Django may be installed for one version, while you’re running another.

Example of the Problem:

You install Django like this:

pip install django

But you run your project like this:

python3 manage.py runserver

In this case, Django might be installed for python, but you’re running python3.

Python gets confused. Then it throws the error.

How to Fix Python Version Mismatch:

Always install Django using the same Python version you use to run your project.

Example:

python3 -m pip install django

Now Django installs into the correct Python version.

Check again:

python3 -m django --version

If it works, you fixed the issue.

Virtual Environments: The Most Common Real-World Cause

This is the number one reason professionals still see this error.

A virtual environment is like a private box for your project. Django installed outside the box cannot be seen inside it.

What Usually Goes Wrong:

You create a virtual environment.
You forget to activate it.
You run Django commands.
Python says Django doesn’t exist.

It’s not rude. It’s just honest.

How to Check If Your Virtual Environment Is Active

When a virtual environment is active, you’ll usually see its name in your terminal.

Example:

(myenv) user@computer:~/project$

If you don’t see that, your environment is not active.

How to Activate a Virtual Environment

On Windows:

myenv\Scripts\activate

On macOS or Linux:

source myenv/bin/activate

Once activated, install Django inside the environment:

pip install django

Now Django belongs to that project and Python can find it.

A Complete Django Setup Example (From Scratch)

Let’s do a clean example so nothing feels abstract.

Create a Project Folder:

mkdir django_test
cd django_test

Create a Virtual Environment:

python -m venv env

Activate the Environment:

Windows:

env\Scripts\activate

macOS / Linux:

source env/bin/activate

Install Django:

pip install django

Create a Django Project:

django-admin startproject mysite
cd mysite

Run the Server:

python manage.py runserver

If the server starts, congratulations. You fixed the error the right way.

When Django Is Installed but Still Not Found

Yes, this happens too.

Sometimes Django is installed correctly, but Python still can’t see it.

The PATH Problem Explained Simply:

Python only looks for packages in certain folders.
If Django is installed outside those folders, Python won’t find it.

To check where Django is installed, run:

pip show django

You’ll see something like:

Location: /usr/local/lib/python3.10/site-packages

Now check which Python you’re using:

which python

If those paths don’t match, that’s your problem.

The fix is to install Django using the exact Python interpreter you’re using.

IDE Issues: VS Code and PyCharm Confusion

Your code editor can also cause this error.

VS Code and PyCharm sometimes use a different Python interpreter than your terminal.

How to Fix It in VS Code:

Open the command palette and select:

Python: Select Interpreter

Choose the Python version inside your virtual environment.

Restart VS Code after selecting it.

Now Django imports will work.

Common Django Import Example That Triggers the Error

Here’s a simple file that causes the error if Django isn’t set up correctly:

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello Django")

If Django is missing or misconfigured, Python will throw the error immediately.

Once Django is properly installed and the environment is active, this code works perfectly.

Final Thoughts

Seeing “ModuleNotFoundError: No module named Django” can feel discouraging, especially when you’re excited to start learning Django. But this error isn’t a sign that you’re doing something wrong it’s just Python telling you it can’t find Django where it’s looking. Once you understand how installations, Python versions, and virtual environments work together, the fix becomes simple and predictable. Treat this error as a learning moment, not a setback. Every Django developer has faced it at least once, and now you know exactly how to handle it with confidence.

author-avatar

About Daniyal Ahmed

Python Developer since 2020 | Release Manager for Python 3.8 | Creator of Black code formatter | Contributing to Python's growth and adoption.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments