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-packageswhere pip installs go - Its own
pipbinary (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:
- How to Fix “Permission Denied” on Linux — chmod, chown, and sudo explained
- apt vs Snap vs Flatpak — Troubleshooting Guide — when each package manager breaks
- How to Fix “Could Not Get Lock /var/lib/dpkg/lock” on Ubuntu — stuck package manager fix
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