ModuleNotFoundError in Python: Causes and Fixes with Simple Examples

 

What is ModuleNotFoundError in Python?

A ModuleNotFoundError in Python occurs when Python cannot find the module you are trying to import.

In simple words, Python is saying:

“I don’t know where this module is.”

This error commonly appears when working with:

  • External libraries

  • Virtual environments

  • Incorrect module names

  • Missing installations


Common ModuleNotFoundError Message

A typical error message looks like this:


ModuleNotFoundError: No module named 'requests'

This means Python tried to import a module called requests, but it is not available in the current environment.


Example 1: Module Not Installed

❌ Incorrect Code


import numpy

❌ Error


ModuleNotFoundError: No module named 'numpy'

✔ Why this happens

  • The module is not installed

  • Python cannot find it in site-packages

✔ Solution

Install the module:


pip install numpy

Then run your program again.


Example 2: Wrong Module Name

❌ Incorrect Code


import reqests

❌ Error


ModuleNotFoundError: No module named 'reqests'

✔ Correct Code


import requests

✔ Always double-check spelling.


Example 3: Module Installed but Still Error

Sometimes the module is installed, but Python still throws an error.

❌ Error


ModuleNotFoundError

✔ Why this happens

  • Using multiple Python versions

  • Installing module in a different environment

✔ Solution

Check Python version:


python --version

Install using the correct Python:


python -m pip install requests

Example 4: ModuleNotFoundError in Virtual Environment

❌ Problem

You installed the module globally, but your virtual environment doesn’t have it.

✔ Solution

Activate virtual environment:


source venv/bin/activate

Then install:


pip install flask

Example 5: Importing Local Python Files

❌ Incorrect Code


import mymodule

❌ Error


ModuleNotFoundError: No module named 'mymodule'

✔ Correct Setup

Make sure:

  • mymodule.py exists

  • It is in the same folder


from mymodule import my_function

How to Check Installed Modules


pip list

Or:


pip show requests


How to Avoid ModuleNotFoundError

✔ Use correct module names
✔ Install modules properly
✔ Use virtual environments
✔ Match Python and pip versions
✔ Keep project structure clean


Debugging Tip

Print Python path:


import sys
print(sys.path)

This shows where Python looks for modules.


Difference Between ImportError and ModuleNotFoundError

Error TypeMeaning
ImportErrorModule found, but import failed
ModuleNotFoundErrorModule not found at all

Summary

ModuleNotFoundError happens when Python cannot locate the requested module.

Once you understand how Python handles imports and environments, this error becomes easy to fix.


FAQ

Q1: Is ModuleNotFoundError a syntax error?
No, it is a runtime error.

Q2: Can pip install fix this error?
Yes, in most cases.

Q3: Is ModuleNotFoundError common?
Yes, especially for beginners.

Comments

Popular posts from this blog

AttributeError in Python: Causes and Fixes with Simple Examples

SyntaxError in Python: Causes and Fixes with Simple Examples

SystemExit in Python: Causes and Fixes with Simple Examples