Cybersecurity and programming, explained plainly

ModuleNotFoundError: pip Said Success. Python Still Cannot Find the Package.

Beginner · Dec 6, 2026 ·By Mohamed Ghareeb
pip Said Success. Python Still Cannot Find the Package.

I ran pip install, it printed Successfully installed, and then the file threw ModuleNotFoundError. Do not reinstall ten times. A successful install tells you the package went into one particular environment. It does not mean every Python on your machine can now see it. Follow the interpreter that runs your file, then check the import name and your own filenames.

Start with the name on the last line

The Python docs describe ModuleNotFoundError as a subclass of ImportError, raised when an import cannot find a module. Read the missing name exactly. If you are importing a large library and the message names some other module, the problem may be one of its dependencies rather than the library you typed. And do not confuse it with AttributeError or cannot import name; those need a different read of the traceback.

1. Which Python is running the file?

Put these lines temporarily at the top of the failing file, before the import, and run it with the same button or command you normally use:

import sys
print(sys.executable)
print(sys.prefix)
print(sys.base_prefix)

The first line is the path of the Python executable. That is the one you follow. If your editor runs one Python and your terminal runs another, installing in the terminal will never fix the editor. Make sure the interpreter selected in your editor points at the environment you mean.

Think of two workbenches in two workshops. You put the screwdriver on the first bench, and the person at the second bench cannot find it. The bench here is the environment's library folder, not the whole machine.

The pip documentation explains that python -m pip runs pip with the interpreter you specified. If python on your machine is the same one your file printed, use:

python -m pip --version
python -m pip show pandas
python -m pip install pandas

Those are terminal commands, not lines to put inside a Python file. pandas is only an example; replace it with the real package name you need. If your command is python3, use it in all of them instead of python. Do not switch between them at random.

pip --version shows the pip version and where the pip module lives, not where the Python executable is. It is normal for those to be different folders. pip show prints the package details including its Location. If it says Package(s) not found, the package is not installed in that environment under that name. And if the editor uses a different path, fix the interpreter choice, or run -m pip with the full path to that interpreter.

2. Inside a virtual environment or outside it?

A venv creates an environment with its own libraries, isolated by default from the system Python's. A package installed outside .venv is not necessarily available inside it. In the diagnosis above, sys.prefix differing from sys.base_prefix is the sign that the interpreter is running from a venv.

If your project already has a .venv, open a terminal in the project folder and activate it with the command for your shell. Do not run all of these:

# Linux / macOS: bash or zsh
source .venv/bin/activate

# Windows: Command Prompt (cmd.exe)
.venv\Scripts\activate.bat

# Windows: PowerShell
.\.venv\Scripts\Activate.ps1

After activating, run python -m pip install pandas, then run the file with the same environment. The environment name usually appears at the start of your prompt, but its absence is not proof you are outside it: prompts are configurable. Trust the interpreter path and sys.prefix, not the look of the line.

Activation is not actually required. You can target the environment's Python directly, which is useful when PowerShell refuses to run the activation script and you would rather not change the machine's policy just to test something:

# Linux / macOS
.venv/bin/python -m pip install pandas

# Windows: cmd.exe or PowerShell
.\.venv\Scripts\python.exe -m pip install pandas

If .venv does not exist, those commands will not create it. To make a new environment from the project folder, run python -m venv .venv, then pick one of the methods above.

3. A notebook has its own kernel

A notebook runs cells in a kernel. Do not assume the Python the kernel uses is the one your terminal uses. In a cell, print sys.executable as in the first example. And in a notebook on an IPython kernel, this command installs into the current kernel, per the IPython docs:

%pip install pandas

The % is deliberate, and this is not a normal Python line you can put in a .py file or a terminal command. If you have the wrong kernel selected, switch to the right environment instead of installing everywhere. And if the install asks you to restart the kernel, do it and re-run the cells you need, because variables held in memory are gone.

4. The install name is not always the import name

There is a difference between the distribution package, the name pip installs, and the import package, the name your code imports. The Python Packaging guide has a clear example: the imaging package is called Pillow, but the import is called PIL.

# Terminal
python -m pip install Pillow

# Inside your Python file
from PIL import Image

Do not take the name after No module named and install it blindly. Check the library's official install page and its import line. A similar name is not proof it is the right package, and you may find an entirely different package under the name you guessed.

5. Sometimes your own file shadows the library

When you run a file normally, Python puts that file's folder at the start of the module search path. If you named your file pandas.py, or made a folder called pandas in the project, it can take precedence over the package you expected. With names like json.py or socket.py the same thing can break imports in other libraries.

Shadowing does not have to produce a literal ModuleNotFoundError. You may see AttributeError, or a partially initialized module, or a message saying the thing you loaded is not a package. To check, print the module's source if the import succeeds:

import pandas
print(pandas.__file__)

If the path points at your own local file instead of the library you meant, rename your file or folder to something clear like data_report.py, and restart the process or kernel. If the import itself fails, the example prints nothing; check your local filenames and the traceback paths first, and do not treat that failure as proof the library is not installed.

A quick diagnosis before you reinstall

  1. Read the missing name on the last line and the traceback above it.
  2. Print sys.executable from the same place that fails.
  3. Run pip through that same interpreter, and check pip show.
  4. In a notebook, check the kernel.
  5. Match the package name to the import name using the official docs.
  6. Check your own project's filenames.

If all of that is clean, keep the full error message and your Python version. There are other causes, such as a missing dependency or an incompatible build, and not every problem is fixed by running the same command again.

Security note: an environment is not a sandbox

A venv separates project libraries and reduces conflicts between them, but it is not a security sandbox. The packages you run can reach files and the network according to your account's permissions. The pip documentation warns that installing distributions can involve running code. Take install commands and package names from their official sources, and do not reach for administrator rights as an automatic fix for an import error.

Pinning a specific version is useful for reproducing the same setup, but it is not a security guarantee, and it does not mean every dependency was pinned too. The goal here is to fix the import with the smallest change needed, not to replace the machine's whole environment.

Related reading: the rest of Python's complaints are decoded in Python Errors Are Telling You What Went Wrong, and a first window to run the fixed code in is in Your First Tkinter Window Is Five Lines Away.

Sources

More in this section
    All Programming posts →

    Comments