By Bonaventure Ogeto|

ModuleNotFoundError in Python: Every Cause and Fix

ModuleNotFoundError means Python looked for a module and could not find it. The fix depends on the cause: a missing pip install, a wrong virtual environment, a typo in the import name, or a file that shadows a built-in module. Below is every common cause, ordered by how often it trips people up, with the exact command or code change to fix each one.

Cause 1: The package is not installed

This is the most common cause. You wrote import requests but never ran pip install requests in the environment your script is using.

Fix:

pip install requests

If you are using Python 3 and your system has both Python 2 and 3, use pip3 install requests instead. You can verify the module is installed by running:

pip list | grep requests

If the module shows up in the list but you still get the error, the problem is almost certainly a virtual environment mismatch (see the next cause).

Cause 2: Wrong virtual environment or no venv active

You installed the package in one virtual environment but your script is running in another, or outside any venv entirely. This happens constantly when you open a new terminal and forget to activate.

Fix: Activate the correct virtual environment before running your script.

# Linux / macOS
source venv/bin/activate

# Windows
venv\Scripts\activate

Check which Python is being used:

which python   # Linux / macOS
where python   # Windows

The path should point inside your project's venv/ directory. If it points to the system Python, your venv is not active.

VS Code users: open the command palette and run "Python: Select Interpreter" to pick the venv interpreter. This saves the setting per workspace so you do not need to remember each time.

Cause 3: Typo in the module name

Python module names are case-sensitive and sometimes differ from the pip package name. A few common examples:

  • pip install Pillow but import PIL (not import Pillow)
  • pip install python-dotenv but import dotenv (not import python_dotenv)
  • pip install opencv-python but import cv2

Fix: Check the package documentation for the correct import name. If unsure, search PyPI for the package and look at the usage examples.

Cause 4: Your own file shadows a module

If you have a file called random.py in your project and you write import random, Python imports your file instead of the built-in random module. This applies to any name collision: json.py, email.py, test.py.

Fix: Rename your file to something that does not collide with a standard library or installed module. Also delete any __pycache__ directory that cached the old import:

mv random.py my_random.py
rm -rf __pycache__

Cause 5: Relative import outside a package

You tried from .utils import helper but ran the file directly with python myfile.py. Relative imports only work inside a proper Python package (a directory with an __init__.py file) and when the package is invoked correctly.

Fix: Either convert the relative import to an absolute one, or run the module as part of a package:

# Instead of: python myapp/main.py
# Run:
python -m myapp.main

If you are working on a simple script that is not part of a package, use absolute imports: from utils import helper.

Cause 6: Python version mismatch

Some packages only support Python 3.8+ or a specific range. If you are running Python 3.6 and the package requires 3.8, pip might install it but the import will fail at runtime with missing syntax features, which can surface as a ModuleNotFoundError in some cases.

Fix: Check your Python version and the package requirements:

python --version
pip show package-name

If there is a mismatch, upgrade Python or install an older version of the package that supports your Python version:

pip install package-name==1.2.3

Frequently Asked Questions

How do I check which modules are installed in my current environment?
Run pip list to see every installed package. Run pip show package-name for details on a specific package, including its location on disk.
Why does the error say No module named but pip says it is installed?
Almost always a virtual environment mismatch. The pip that installed the package and the python that runs your script are pointing to different environments. Run which python and which pip (or where on Windows) and confirm they share the same path prefix.
Does ModuleNotFoundError mean the same thing as ImportError?
ModuleNotFoundError is a subclass of ImportError, added in Python 3.6. It specifically means the module could not be found at all, while ImportError can also mean the module was found but something inside it failed to load.

Ready to build real-world apps?

Join the McTaba Labs full-stack marathon. Ship 8 production apps with M-Pesa, USSD, and WhatsApp integrations, and get career support until placement.

See Programs