How to Fix "ModuleNotFoundError: No module named" in Python on Linux (The Real Reason pip Install Didn't Work)

By Adhen Prasetiyo

Friday, February 20, 2026 • 6 min read

Linux terminal showing Python ModuleNotFoundError error message with Python logo

You open your terminal, install a package:

pip install requests

It says “Successfully installed requests.” Great. Then you run your Python script:

python3 script.py

And Python hits you with:

ModuleNotFoundError: No module named 'requests'

You just installed it. You watched it succeed. What is going on?

Here’s the answer that took me way too long to learn: pip and python3 might be pointing to completely different Python installations. You installed the package to one Python, and you’re running your script with another.

The 30-Second Diagnostic

Run these two commands right now:

pip --version

python3 --version

Look at the output carefully. pip will show something like:

pip 24.0 from /usr/lib/python3.10/site-packages/pip (python 3.10)

And python3 might show:

Python 3.12.3

See the problem? pip installed requests into Python 3.10, but your script runs on Python 3.12. Python 3.12 has its own separate site-packages directory, and it has no idea that requests exists in 3.10’s folder.

This is the single most common cause of “No module named” errors on Linux, and almost nobody explains it properly.

Why This Happens on Linux

Unlike Windows and macOS, Linux systems often have multiple Python versions installed simultaneously. Your system might have Python 3.10 (used by system tools), Python 3.12 (installed later), and maybe even a Python 3.11 from a PPA.

Each Python version has its own:

  • /usr/lib/python3.X/ directory
  • Its own site-packages where pip installs go
  • Its own pip binary (which may or may not be symlinked)

The commands pip, pip3, python, and python3 are just symlinks — and they don’t always point to the same version.

The Fix: Always Use python -m pip

Instead of:

pip install requests

Always use:

python3 -m pip install requests

The -m pip flag tells Python: “use the pip module that belongs to THIS specific interpreter.” This guarantees the package installs to the same Python you’ll use to run your code.

Make this a habit. It eliminates the pip-python mismatch entirely.

To verify, check where the package installed:

python3 -m pip show requests

The “Location” field should match your Python’s site-packages path.

The Permanent Fix: Virtual Environments

A virtual environment creates an isolated Python installation for your project. Inside it, python, pip, and all installed packages are self-contained. No mismatch possible.

Create a virtual environment:

python3 -m venv myproject

Activate it:

source myproject/bin/activate

Your terminal prompt changes to show (myproject). Now you’re inside the environment.

Install packages:

pip install requests pandas flask

These packages exist only inside myproject/. They don’t affect your system Python or any other project.

Run your script:

python script.py

Everything uses the same interpreter. No “No module named” errors.

Deactivate when done:

deactivate

“But I Already Installed It and It Still Doesn’t Work”

If you’ve installed a package and it still throws ModuleNotFoundError, run through this checklist:

1. Verify the package name is correct

Some package names differ between pip and import:

pip install name import name
Pillow PIL
beautifulsoup4 bs4
scikit-learn sklearn
python-dateutil dateutil
opencv-python cv2

The pip package name and the Python import name are not always the same. Check the package’s documentation if you’re unsure.

2. Check you’re running the right interpreter

If you’re using an IDE like VS Code or PyCharm, it might be using a different Python interpreter than your terminal.

VS Code: Press Ctrl+Shift+P → type “Python: Select Interpreter” → choose the correct one (or your virtual environment)

PyCharm: Settings → Project → Python Interpreter → select the correct one

3. Check if you’re inside a virtual environment

Your terminal prompt should show (venv_name) if you’re inside a virtual environment. If you installed packages in a venv but then deactivated it and ran your script, the packages aren’t available.

# Check if you're in a venv

which python3

If it shows /usr/bin/python3, you’re using the system Python. If it shows something like /home/user/myproject/bin/python3, you’re in a virtual environment.

4. Check for circular imports

If you have a file named the same as a module you’re importing, Python will import your file instead. For example, if you create a file called requests.py and then do import requests, Python imports your file — not the pip package.

Fix: Rename your file to something else. Never name your Python files after popular packages.

Linux-Specific Gotcha: Split Standard Library Packages

On Ubuntu and Debian, Python’s standard library is split into separate system packages to save disk space. This means some modules that “should” be included with Python are actually missing until you install them.

Common ones:

# tkinter (GUI toolkit)

sudo apt install python3-tk

# venv (virtual environments)

sudo apt install python3-venv

# distutils (build tools)

sudo apt install python3-distutils

# dev headers (needed to compile C extensions)

sudo apt install python3-dev

If import tkinter fails with ModuleNotFoundError on Ubuntu, it’s not a pip issue — it’s a system package issue. Install it with apt, not pip.

The python3 -m pip Cheat Sheet

What you want Command
Install a package python3 -m pip install package_name
Install specific version python3 -m pip install package_name==1.2.3
Install from requirements file python3 -m pip install -r requirements.txt
Show where a package is installed python3 -m pip show package_name
List all installed packages python3 -m pip list
Check which pip you’re using python3 -m pip --version
Upgrade a package python3 -m pip install --upgrade package_name

If You Found This Guide Helpful

Check out our other troubleshooting resources:

Frequently Asked Questions

Why does pip install succeed but Python still can’t find the module?

Because pip and python might point to different Python installations. pip installs the package to one interpreter’s site-packages, but your script runs on a different interpreter that has its own empty site-packages. Always use python3 -m pip install to ensure they match, or use a virtual environment.

Can I have multiple virtual environments on the same machine?

Yes, as many as you want. Each virtual environment is just a folder. You can have one per project, and they don’t interfere with each other. Each has its own installed packages, and activating one deactivates the previous one.

I get “No module named venv” when trying to create a virtual environment. Why?

On Ubuntu and Debian, the venv module is packaged separately. Install it with sudo apt install python3-venv first, then retry python3 -m venv myproject.

ModuleNotFoundError appears only when I run the script with sudo. Why?

Running with sudo uses root’s Python environment, which is different from your user environment. If you installed packages as your normal user (or in a venv), root can’t see them. Either install the package with sudo python3 -m pip install, or better yet, don’t use sudo to run Python scripts — restructure your code so it doesn’t need root privileges.

Last updated: February 2026 | Tested on Ubuntu 24.04 LTS, Debian 12, Fedora 41 — Python 3.11, 3.12, 3.13

Step-by-Step Guide

1

Check which Python pip is installing to

Run pip --version or pip3 --version in your terminal. Note the Python version and path it shows. Then run python3 --version and which python3. If the paths don't match, pip is installing packages to a different Python than the one you're running.

2

Install using python -m pip instead

Always use python3 -m pip install package_name instead of just pip install. This guarantees the package installs to the exact Python interpreter you're using to run your code.

3

Create a virtual environment

Run python3 -m venv myproject and then source myproject/bin/activate. Install packages inside this environment. This isolates your project and eliminates interpreter mismatch issues entirely.

4

Verify the module is importable

After installing, run python3 -c "import module_name" to verify. If this works but your script still fails, check that your script runs with the same Python interpreter.

5

Check for Linux split packages

On Ubuntu and Debian, Python standard library modules like tkinter and venv are in separate system packages. Install them with sudo apt install python3-tk or python3-venv as needed.

Frequently Asked Questions

Q1: I installed the package with pip but Python still says No module named. Why?
A1: The most common cause is that pip and python point to different Python installations. On many Linux systems, pip might install to Python 3.10 while your script runs on Python 3.12. Use python3 -m pip install to ensure the package goes to the correct interpreter, or use a virtual environment.
Q2: What is the difference between pip and pip3?
A2: On older systems, pip points to Python 2 and pip3 points to Python 3. On newer systems where Python 2 is removed, both may point to Python 3 but potentially different minor versions. The safest approach is to always use python3 -m pip which guarantees you are using the pip tied to your specific Python 3 interpreter.
Q3: Do I really need a virtual environment?
A3: For anything beyond a quick one-file script, yes. Virtual environments prevent interpreter mismatches, avoid conflicts between projects that need different package versions, and since Python 3.12 on Ubuntu, pip refuses to install system-wide anyway due to PEP 668. Virtual environments are now the standard way to work with Python on Linux.
Q4: Why does import tkinter fail even though it is part of the Python standard library?
A4: On Debian and Ubuntu, several standard library modules are split into separate system packages to save space. Tkinter requires sudo apt install python3-tk. Similarly, venv requires python3-venv and distutils requires python3-distutils. These are installed with apt, not pip.
Adhen Prasetiyo

Research Bug bounty at javahack team

Research Bug bounty Profesional

Web Development Research Bug Hunter
View all articles →