Module Not Found – “No Module Named” in Python

This error means Python can’t find the module you're trying to import. It usually happens due to installation issues, incorrect import paths, or virtual environment misconfigurations.

When you see the error:

ModuleNotFoundError: No module named 'xyz'
Python is telling you that it couldn’t locate the specified module in its current environment or path. Let’s break down the common causes and how to fix them.

1. The Module Isn’t Installed

Most often, the module simply hasn't been installed in your environment.

Fix

Install the module using pip:

pip install module_name

If you're using Python 3 specifically:

pip3 install module_name
For virtual environments, activate your environment first before installing.

2. You Installed It for the Wrong Python Version

If you have multiple versions of Python installed (like Python 2.x and 3.x), the module may be installed for one, but not the one you're running.

Fix

Check which Python you're using:

python --version

Then ensure you're installing the module for that version. If needed:

python3 -m pip install module_name

3. You’re Using a Virtual Environment but Didn’t Activate It

If the module is installed in a virtual environment but the environment isn't activated, Python won't find the module.

Fix

Activate your virtual environment:

# For Windowsvenv\Scripts\activate   # For macOS/Linux source venv/bin/activate

4. Incorrect Import Statement or File Structure

Sometimes the issue is due to how the import statement is written or how your files are organized.

Fix

Check:

  • The module name (case-sensitive)
  • Whether the module is actually available in the directory
  • Whether __init__.py is missing in submodules
Also Read: Python Developer Hourly Rate in 2025

5. Your PYTHONPATH Is Misconfigured (Advanced)

If you've manipulated PYTHONPATH, or you're running code from an unexpected directory, Python may not locate the module.

Fix

Print your current paths:

python
import sysprint(sys.path)

You can temporarily add a path:

python
import syssys.path.append("/path/to/module")

Tip

If you're working with packages or running scripts from outside the project root, try using python -m module_name instead of directly calling the script.
Related

Amazon CloudWatch allows you to run log insights queries using the logs client in Boto3. Below is a step by step guide to querying logs…

28 Oct, 2025

The Kalman Filter is an efficient recursive algorithm used to estimate the state of a system from noisy measurements. In computer vision, it’s commonly used…

21 Oct, 2025

Printing exceptions in python helps to debug issues by giving clear information about what went wrong and where. You can access the exception message or…

15 Oct, 2025