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:

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.