CalcSnippets Search
Python 2 min read

`ModuleNotFoundError` in Python Is Often Not a Missing Package, It’s a “You Installed It Into One Interpreter and Ran Another One” Problem

A practical guide to fixing Python `ModuleNotFoundError` by checking the active interpreter, virtual environment, and pip target instead of reinstalling the same package blindly.

Why this error matters: many Python import failures are not about whether a package exists. They are about whether the interpreter you are running is the same one your installer used.

A typical failure looks like:

ModuleNotFoundError: No module named 'requests'

People often respond by running:

pip install requests

again and again until they have installed the same package into three different environments and fixed nothing.

The real first question

Ask:

  1. which Python interpreter is running the code?
  2. which interpreter did pip install into?

Those are not always the same.

Check the interpreter and pip target

Run:

which python
which python3
python3 -V
python3 -m pip -V

The last command is especially useful because it tells you the path of the interpreter-backed pip.

If you see:

pip 24.x from /some/path/site-packages/pip (python 3.12)

but your app is running under another Python path or another version, you have found the mismatch.

Use module installs through the interpreter

Prefer:

python3 -m pip install requests

not:

pip install requests

because the first form explicitly binds the installation to the interpreter you name.

If you are using a virtual environment, verify it is active

Check:

echo $VIRTUAL_ENV
which python
python3 -m pip -V

If the environment is not active, reactivate it:

source .venv/bin/activate

Then reinstall inside that environment if needed:

python -m pip install -r requirements.txt

Inspect whether the module is actually installed there

python3 -m pip show requests

If it is installed, but your script still cannot import it, the wrong interpreter is almost certainly executing your code.

IDE and task runner traps

The terminal may use one interpreter while:

  • VS Code
  • PyCharm
  • a task runner
  • a notebook kernel

uses another.

So if the terminal import works but the app still fails, inspect the editor or runtime interpreter setting instead of reinstalling packages pointlessly.

A reliable reset sequence

python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python your_script.py

That recreates a known environment and removes a lot of interpreter ambiguity.

Final recommendation

Treat ModuleNotFoundError as an interpreter alignment issue first. Verify the active Python, install through python -m pip, and make sure your editor, shell, and runtime all point to the same environment before doing anything more dramatic.

Sources

Keep reading

Related guides