Quick answer: ModuleNotFoundError almost always means the package was installed into a different Python environment than the one running your script — not that it is missing from the system. Always install with python3 -m pip install, never bare pip. If pip reports success but the error persists: run python3 -m pip show <package>, note the Location path, then run python3 -c "import sys; print(sys.path)" — if Location is not in sys.path, you installed into the wrong interpreter. ImportError with a .so filename means a missing system library — fix with sudo apt install, not pip. Tested on Python 3.10, 3.11, 3.12, Ubuntu 22.04, Windows 11 WSL2, March 2026.
Python dependency errors stop you dead. The error message tells you exactly what is missing, but installing the package and still getting the same error is one of the most common Python frustrations — and it almost always comes down to installing into the wrong environment. This guide covers how to fix ModuleNotFoundError and ImportError correctly, why the environment mismatch happens, and the specific cases where a simple pip install is not enough. Tested on Python 3.10, 3.11, and 3.12 on Ubuntu 22.04 and Windows 11 with WSL2. Last updated March 2026.
If you are running Python inside WSL2 and hitting dependency issues, check the WSL2 installation guide first — a misconfigured WSL2 environment is sometimes the actual cause. For managing Python packages at scale see the Python dependency management guide.
What is the difference between ModuleNotFoundError and ImportError, and why does it matter for the fix?
These two errors require different fixes — ModuleNotFoundError is solved by installing the package into the correct environment, while ImportError from a .so filename means a missing system library that pip cannot install.
| Error type | What it means | Fix |
|---|---|---|
ModuleNotFoundError: No module named 'X' | Package not installed in the active Python environment | python3 -m pip install X — but check environment first |
ImportError: cannot import name 'X' from 'Y' | Package installed but version is wrong or dependency missing | pip install --upgrade Y or reinstall with --force-reinstall |
ImportError: libX.so.1: cannot open shared object file | Missing system C library — pip cannot fix this | sudo apt install <system-package> |
ModuleNotFoundError — Python could not find the module at all. It is not installed in the environment currently running your script, or it is installed somewhere Python is not looking.
ModuleNotFoundError: No module named 'requests'
ImportError — Python found the module but failed while loading it. This happens when a package is installed but one of its own dependencies is missing, when there is a version mismatch, or when a compiled extension (.so or .pyd file) cannot load because a system library is absent.
ImportError: cannot import name 'DataFrame' from 'pandas'
ImportError: libgomp.so.1: cannot open shared object file: No such file or directory
The second ImportError example is from a missing system library — pip install alone will not fix it. That case is covered in its own section below.
Why do I keep getting ModuleNotFoundError after running pip install?
pip installed the package into a different Python interpreter than the one running your script — find out which interpreter is active before installing anything, or you will repeat this cycle every time.
# Linux / macOS / WSL2
which python3
which pip3
# Windows PowerShell
where python
where pip
Expected output when a virtual environment is active:
/home/username/myproject/venv/bin/python3
/home/username/myproject/venv/bin/pip3
Expected output when no virtual environment is active:
/usr/bin/python3
/usr/bin/pip3
If which python3 and which pip3 point to different locations, anything you install with pip goes to a different environment than the one running your script. The permanent fix: always install through the interpreter itself using -m pip:
# This guarantees pip installs into the same Python that runs your script
python3 -m pip install requests
Use python3 -m pip instead of bare pip or pip3 as a permanent habit. It eliminates the mismatch entirely by making pip use the same interpreter that python3 points to.
How do I install a missing Python package and verify it worked?
python3 -m pip install requests
Expected output:
Collecting requests
Downloading requests-2.31.0-py3-none-any.whl (62 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.6/62.6 kB 1.2 MB/s eta 0:00:00
Installing collected packages: requests
Successfully installed requests-2.31.0
Always verify the install worked before running your script — this tells you immediately whether the environment mismatch is still present:
python3 -c "import requests; print(requests.__version__)"
2.31.0
If this verify command still throws ModuleNotFoundError after a successful install, the interpreter running the verification is different from the one pip installed into. Go back to the previous step and check your interpreter paths.
Which pip install name do I use when the import name is different?
Several common packages have a different name on PyPI than the name you use in import — searching PyPI for the import name finds nothing, which looks like the package does not exist:
| Error: No module named… | Correct install command |
|---|---|
cv2 | pip install opencv-python |
PIL | pip install Pillow |
sklearn | pip install scikit-learn |
yaml | pip install PyYAML |
bs4 | pip install beautifulsoup4 |
dateutil | pip install python-dateutil |
When the module name in your error does not match anything on PyPI, search PyPI for the likely package name rather than the import name. The correct package’s PyPI page will show the import name in the documentation or README.
How do I fix ModuleNotFoundError when pip show says the package is already installed?
Compare where pip installed the package to where Python is searching — if those two paths do not overlap, Python will never find the package regardless of how many times you reinstall it.
Check where pip installed the package:
python3 -m pip show requests
Name: requests
Version: 2.31.0
Location: /home/username/myproject/venv/lib/python3.11/site-packages
Check what paths Python is actually searching:
python3 -c "import sys; print('\n'.join(sys.path))"
/usr/lib/python310.zip
/usr/lib/python3.10
/usr/lib/python3.10/lib-dynload
/usr/local/lib/python3.10/dist-packages
/usr/lib/python3/dist-packages
If the Location from pip show does not appear anywhere in the sys.path output, Python will never find the package. The cause is a different Python version running your script than the one pip installed into — most commonly Python 3.10 vs 3.11 on a system with both installed. Fix:
# Find all python3 interpreters on the system
which -a python3
# Run your script with the specific interpreter that has the package
/home/username/myproject/venv/bin/python3 your_script.py
How do I fix ImportError for a missing .so system library that pip cannot install?
When the ImportError message contains a .so filename, the missing dependency is a system C library — install it with apt, not pip. pip only installs Python packages and cannot install system libraries.
ImportError: libgomp.so.1: cannot open shared object file: No such file or directory
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
Common system libraries and the apt packages that provide them:
# libgomp.so.1 — needed by numpy, scikit-learn, lightgbm
sudo apt install libgomp1
# libGL.so.1 — needed by opencv-python
sudo apt install libgl1
# libXext.so.6 — needed by some GUI-related packages
sudo apt install libxext6
# Build tools — needed for packages that compile from source
sudo apt install build-essential python3-dev
After installing the system library, the Python import works without reinstalling the Python package.
How do I find which apt package provides a specific .so file?
# If apt-file is not installed
sudo apt install apt-file
sudo apt-file update
# Find which package provides a specific .so file
apt-file search libgomp.so.1
Expected output:
libgomp1: /usr/lib/x86_64-linux-gnu/libgomp.so.1
libgomp1: /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0
The package name is on the left — libgomp1 in this example. Install it with sudo apt install libgomp1.
Why does every Python project need its own virtual environment?
Installing packages globally eventually breaks things — two projects requiring different versions of the same library cannot coexist in the same global environment, and the first time you install conflicting packages, one project stops working with no clear error message pointing to the version conflict.
# Create a virtual environment for a project
cd ~/myproject
python3 -m venv venv
# Activate it — Linux / macOS / WSL2
source venv/bin/activate
# Activate it — Windows PowerShell
.\venv\Scripts\Activate.ps1
After activation your prompt changes to show the environment name:
(venv) username@machine:~/myproject$
Every pip install now goes into this project’s environment only. When you are done:
deactivate
The mistake that catches beginners every time: opening a new terminal window deactivates the virtual environment. You have to run source venv/bin/activate again in each new terminal. If you forget and run pip install in the new terminal, the package goes to the global environment — which is exactly the mismatch this whole guide is about.
How do I save and reproduce my Python environment on another machine?
# Save current environment to requirements.txt
python3 -m pip freeze > requirements.txt
# Install from requirements.txt in a new environment
python3 -m pip install -r requirements.txt
pip freeze includes every package including transitive dependencies — 50+ lines even for a simple project. For production deployments where you need reproducible builds, exact pins from pip freeze (requests==2.31.0) are correct. For libraries you share with others, list only your direct dependencies with loose version pins (requests>=2.28) so users are not locked to your exact transitive dependency tree.
How do I fix a corrupted or broken Python package installation?
A corrupted install produces an ImportError that looks like an internal Python error rather than a missing dependency — the package is present but its files are damaged:
ImportError: cannot import name '_imaging' from 'PIL'
Force reinstall downloads fresh files and replaces the broken ones:
# Force reinstall
python3 -m pip install --force-reinstall Pillow
# If that still fails, clear pip cache first — cached files may also be corrupt
python3 -m pip cache purge
python3 -m pip install --force-reinstall Pillow
Expected output from pip cache purge:
Files removed: 23
How do I fix ModuleNotFoundError inside a Jupyter notebook?
Jupyter notebooks run their own kernel which is a separate Python environment from your terminal — installing a package in your terminal and finding it missing in Jupyter is the same environment mismatch problem, just harder to see.
Check which Python Jupyter is using from inside a notebook cell:
import sys
print(sys.executable)
/home/username/myproject/venv/bin/python3
Install the package into that exact interpreter from inside the notebook — this guarantees the install goes to the same Python the kernel is using, regardless of which environment your terminal has active:
import subprocess
import sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
Restart the kernel after installing, then import normally. Do not use !pip install in notebook cells — the ! shell command does not guarantee it uses the kernel’s Python interpreter.
Why does sudo pip install break Ubuntu and what should I do instead?
On Ubuntu 22.04 and newer, pip refuses to install into the system Python and shows this error — this is intentional, not a bug:
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, use apt.
hint: See PEP 668 for the full story.
The correct responses, in order of preference:
# Best: use a virtual environment (correct for all project work)
python3 -m venv venv && source venv/bin/activate
pip install requests
# If you genuinely need a package system-wide: use apt where available
sudo apt install python3-numpy
# Search what Python packages apt has
apt search python3-
For more on structuring Python dependency management across projects see the Python dependency management guide. For running Python on a Linux environment under Windows see the WSL file transfer guide — moving project files incorrectly between Windows and WSL2 causes a separate class of environment issues.
Leave a Reply