Last modified: Oct 01, 2026

Fix ModuleNotFoundError After pip install

You just ran pip install. Everything looked fine. Then you run your script and get ModuleNotFoundError. This is one of the most common and most confusing errors in Python.

The good news is that this error is almost always fixable. In most cases, it comes down to one simple mismatch. Let's walk through the causes and the fixes step by step.

What Does ModuleNotFoundError Mean?

This error means Python cannot find the module you are trying to import. Python looks for modules in specific places. If the module is not in one of those places, the import fails.

Here is a typical example:


# main.py
import requests  # trying to import the requests library

print(requests.__version__)

Traceback (most recent call last):
  File "main.py", line 1, in 
    import requests
ModuleNotFoundError: No module named 'requests'

This looks strange. You installed requests. So why can't Python find it?

Cause 1: You Installed the Package in the Wrong Environment

This is the number one cause. Your computer can have many Python installations. It can also have many virtual environments.

When you run pip install requests, pip installs into one environment. When you run your script with python main.py, it may use a different one.

So the package exists, but not where your script is looking.

How to Check Your Python and pip Paths

Run these commands to see which Python and pip you are using:


which python
which pip

On Windows, use this instead:


where python
where pip

If the paths do not match, that is your problem. The fix is to install using the same Python that runs your script.


python -m pip install requests

Using python -m pip is the safest way. It guarantees that pip installs into the same environment as that Python interpreter.

Cause 2: You Are Not Inside Your Virtual Environment

Virtual environments keep project dependencies separate. If you created one but forgot to activate it, your installs and runs will not line up.

Activate your environment first. On macOS and Linux:


source venv/bin/activate

On Windows:


venv\Scripts\activate

After activation, your prompt shows the environment name. Now install and run your script again.


python -m pip install requests
python main.py

2.31.0

It works now. The key is to install and run in the same active environment.

Cause 3: The Package Name Differs From the Import Name

Some packages have a different name for install and import. This trips up many beginners.

For example, you install Pillow but import PIL. You install scikit-learn but import sklearn.


python -m pip install Pillow

# correct import name for Pillow
from PIL import Image

print(Image.__name__)

Image

Always check the package documentation. It tells you the correct import name.

Cause 4: Typo in the Module Name

Sometimes the fix is as simple as a spelling mistake. Python is case sensitive. Requests and requests are not the same.


# wrong: capital R
import Requests

ModuleNotFoundError: No module named 'Requests'

Fix the spelling and the error goes away.


# correct: all lowercase
import requests

print("Import successful")

Import successful

Cause 5: A Local File Shadows the Package

Python checks the current directory first. If you have a file named requests.py, it will import that file instead of the real package.

This is called shadowing. It is a sneaky bug.

Never name your files the same as popular libraries. Rename the file and delete any leftover .pyc files.

Cause 6: pip Is Outdated or Broken

An old pip version can fail to install packages correctly. Upgrade it first.


python -m pip install --upgrade pip

Successfully installed pip-24.0

Then reinstall your package and try the import again.

Cause 7: Multiple Python Versions on Your System

Many machines have Python 3.10, 3.11, and 3.12 installed at once. Each one has its own packages.

You may install for one version but run another. Check the version you are running:


python --version

Python 3.12.1

Then install using that exact interpreter:


python3.12 -m pip install requests

This removes all doubt about which Python gets the package.

Cause 8: IDE Uses a Different Interpreter

Your terminal may work fine, but your editor does not. Tools like VS Code and PyCharm have their own interpreter setting.

Open your IDE settings. Find the Python interpreter option. Point it to the same environment where you installed the package.

After that, restart the editor. The import error usually disappears.

Quick Checklist to Fix the Error

Follow these steps in order. Most people solve the problem within the first three.

Step 1: Confirm the package is installed with python -m pip show requests.

Step 2: Check that pip and Python point to the same environment.

Step 3: Activate your virtual environment before installing and running.

Step 4: Verify the import name matches the package name.

Step 5: Check for typos and shadowing files.

Step 6: Upgrade pip and reinstall the package.

Step 7: Set the correct interpreter in your IDE.

Example: Full Working Setup

Here is a complete flow that avoids the error from the start.


# create a virtual environment
python -m venv venv

# activate it (macOS or Linux)
source venv/bin/activate

# install the package into this environment
python -m pip install requests

# run the script with the same interpreter
python main.py

# main.py
import requests  # now found in the active environment

response = requests.get("https://api.github.com")
print(response.status_code)  # prints the HTTP status code

200

This pattern works every time. Create, activate, install, and run. Keep all four steps in the same environment.

Conclusion

ModuleNotFoundError after pip install is rarely a real bug. It is almost always an environment mismatch.

The package is installed somewhere. Your script is looking somewhere else. Once you align the two, the error goes away.

Use python -m pip for every install. Always activate your virtual environment. Check the import name in the docs. Set the right interpreter in your IDE.

Follow the checklist above and you will spend less time debugging and more time coding.