Python  

How to Fix ModuleNotFoundError in Python After Installing a Package

Introduction

One of the most common errors Python developers face — especially beginners — is the ModuleNotFoundError, even after installing a package correctly using pip. This error simply means Python cannot find the module you are trying to import. But the real reason behind this can vary. In many cases, the package is installed, but Python is looking in the wrong place.

What Does ModuleNotFoundError Mean?

Python throws this error when it cannot locate the module you are trying to import. Example:

import pandas

If pandas is not available in the environment Python is using, you will see:

ModuleNotFoundError: No module named 'pandas'

Even if you installed pandas, Python may still not find it due to environment mismatches or incorrect installation.

1. You Installed the Package in a Different Python Environment

This is the most common reason. Your system may have multiple Python versions:

  • Python 3.8

  • Python 3.9

  • Python 3.10

Or you may have multiple tools like:

  • Anaconda

  • Virtual environments

  • VS Code Python interpreter

How to Check Which Python You Are Using

Run:

python --version

Or

python3 --version

How to Check Where pip Is Installing Packages

Run:

pip show package_name

Example:

pip show pandas

This shows the installation path.

Fix

Use the correct pip version for your Python version:

python3 -m pip install package_name

Or for Windows:

py -3.10 -m pip install package_name

2. You Installed the Package Using pip, but Your Editor Uses a Different Interpreter

Tools like VS Code, PyCharm, Jupyter Notebook, and Spyder may use a different Python interpreter from the one where packages were installed.

Fix in VS Code

  • Press Ctrl + Shift + P

  • Search Python: Select Interpreter

  • Choose the environment where the package is installed

Fix in PyCharm

  • Go to Settings → Project → Python Interpreter

  • Select the correct environment

A wrong interpreter will always lead to ModuleNotFoundError.

3. You Forgot to Activate Your Virtual Environment

If you installed a package inside a virtual environment but did not activate it, Python outside the environment cannot access that package.

Activate on Windows

venv\Scripts\activate

Activate on macOS/Linux

source venv/bin/activate

After Activation

Run your Python script again.

4. The Package Name You Are Importing Is Incorrect

Sometimes the package installation name and import name are different.

Examples:

  • Install name: beautifulsoup4

  • Import name: from bs4 import BeautifulSoup

Another example:

  • Install name: python-dateutil

  • Import name: import dateutil

Fix

Search the correct import name or try:

import package_name

5. The Package Failed to Install Properly

Sometimes installation shows warnings or partial installation.

Fix

Reinstall the package:

pip uninstall package_name
pip install package_name

If it’s a permissions issue:

pip install package_name --user

6. You Are Running the Script From a Directory With the Same Name as a Module

Example:
If your file is named:

pandas.py

And you write:

import pandas

Python will try to import your file instead of the actual package.

Fix

Rename your file.

7. Python Path Issues

Python uses the PYTHONPATH variable to locate modules. If Python paths are broken, imports will fail.

Check Python Path

import sys
print(sys.path)

If the installed package path is missing, Python cannot find it.

Fix

Add the path manually:

import sys
sys.path.append("/path/to/site-packages")

8. Jupyter Notebook Using Different Kernel

Installing a package in terminal does NOT mean Jupyter Notebook can access it.

Fix

Install packages inside Jupyter:

!pip install package_name

Or install into the exact kernel using:

python -m ipykernel install --user

Summary of the Most Reliable Fix

The best universal fix is to install the package using the Python interpreter you are running:

python -m pip install package_name

This ensures pip installs the package in the correct environment.

Conclusion

ModuleNotFoundError in Python usually happens because Python cannot find the package you installed — often due to using the wrong interpreter, wrong environment, or incorrect import name. By checking your Python version, verifying pip installation paths, selecting the right interpreter in your editor, and activating virtual environments, you can fix this error quickly. Once you understand how Python environments and paths work, resolving import issues becomes simple and predicta