Fix: ModuleNotFoundError: No module named in Python
The Error
You try to run a Python script and get this:
Python 3:
Traceback (most recent call last):
File "app.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'Python 2 (or early Python 3):
Traceback (most recent call last):
File "app.py", line 1, in <module>
import requests
ImportError: No module named requestsBoth mean the same thing: Python can’t find the module you’re trying to import. ModuleNotFoundError was introduced in Python 3.6 as a subclass of ImportError, so older Python versions raise ImportError instead.
Why This Happens
Python looks for modules in a specific list of directories (stored in sys.path). When the module isn’t in any of them, you get this error.
The most common causes:
- The package isn’t installed. You’re importing a third-party package that was never installed with pip.
- You installed the package with a different Python or pip. Your system has multiple Python versions, and you ran
pip installfor one but are running the script with another. - Your virtual environment isn’t activated. The package is installed in a venv, but you’re running Python outside of it (or vice versa).
- The package name doesn’t match the import name. You install
Pillowbut importPIL. You installopencv-pythonbut importcv2. This mismatch is a frequent source of confusion. - The Python version doesn’t support the package. The package requires a newer (or older) Python than what you have.
Fix 1: Install the Package
If you haven’t installed the package yet, install it with pip:
pip install requestsIf you’re not sure whether it’s installed, check:
pip show requestsThis prints the package’s version and install location. If it says WARNING: Package(s) not found, the package isn’t installed.
You can also list all installed packages:
pip listFix 2: Use the Correct pip (pip vs pip3)
This is the most common cause of “I installed it but Python still can’t find it.” You installed the package with one Python version but are running the script with another.
On many systems, pip is linked to Python 2 and pip3 is linked to Python 3. Or pip might point to a different Python 3 installation than the one you’re using.
The safest approach: Always use python -m pip instead of bare pip:
python -m pip install requestsThis guarantees you’re installing the package for the exact Python interpreter that python points to. If you run your script with python3, use:
python3 -m pip install requestsHow to verify which Python pip is using
Check where pip is installing packages:
pip --versionOutput:
pip 24.0 from /usr/lib/python3.11/site-packages/pip (python 3.11)Now check which Python you’re running:
python --version
python3 --versionIf pip --version says Python 3.11 but python --version says Python 3.12, that’s your problem. The package is installed for 3.11 but you’re running 3.12.
Use python3.12 -m pip install requests to install for the correct version.
Fix 3: Activate Your Virtual Environment
If you created a virtual environment, packages installed inside it are only available when the environment is active. Running pip install outside the venv installs packages globally (or to your user site-packages), and your venv won’t see them.
venv (built-in)
# Create (if you haven't already)
python -m venv .venv
# Activate
# Linux/macOS:
source .venv/bin/activate
# Windows (Command Prompt):
.venv\Scripts\activate.bat
# Windows (PowerShell):
.venv\Scripts\Activate.ps1
# Now install
pip install requestsWhen the venv is active, your shell prompt will show the environment name (e.g., (.venv)). If you don’t see it, the venv isn’t active.
Conda
# Activate your environment
conda activate myenv
# Install
conda install requests
# or
pip install requestsPoetry
Poetry manages its own virtual environment. Use poetry run to run commands inside it:
poetry add requests
poetry run python app.pyOr activate the shell:
poetry shell
python app.pyCommon mistake: installing before activating
If you run pip install requests without activating your venv first, the package goes to the system Python. Then when you activate the venv and run your script, Python looks in the venv’s site-packages and doesn’t find it. Always activate first, then install.
Fix 4: Fix the Module/Import Name
Some Python packages have a pip install name that’s different from the import name. This is one of Python packaging’s most confusing quirks.
For example:
pip install Pillow# Wrong -- the package is called Pillow, but the import is PIL
import Pillow # ModuleNotFoundError
# Correct
from PIL import ImageCommon Package Name Mismatches
| pip install name | import name | Notes |
|---|---|---|
Pillow | PIL | Pillow is the maintained fork of PIL |
opencv-python | cv2 | Also applies to opencv-contrib-python |
scikit-learn | sklearn | |
python-dateutil | dateutil | |
beautifulsoup4 | bs4 | |
python-dotenv | dotenv | |
PyYAML | yaml | |
pymysql | pymysql | Install name is PyMySQL (case differs) |
attrs | attr | import attr (though import attrs also works since v22) |
google-cloud-storage | google.cloud.storage | Namespace package |
python-magic | magic | |
Faker | faker | Case difference |
If you get ModuleNotFoundError, check the package’s PyPI page or documentation for the correct import name. The install name and import name are not always the same.
Fix 5: Check Python Version Compatibility
Some packages drop support for older Python versions. Others only support Python 3 and won’t install on Python 2.
Check the package’s required Python version on PyPI:
pip install requests==This will show an error listing all available versions. Or check the project’s PyPI page for the “Requires Python” field.
If you need a specific Python version:
# Check your version
python --version
# Install a specific package version that supports your Python
pip install numpy==1.24.0For example, NumPy 2.0+ requires Python 3.9+. If you’re on Python 3.8, you need to install an older NumPy version:
pip install "numpy<2"Edge Cases
Relative import errors
If you see ImportError: attempted relative import with no known parent package alongside a relative import (like from .utils import helper), the issue is that you’re running the file directly instead of as part of a package.
# my_package/main.py
from .utils import helper # Fails if you run: python my_package/main.pyRelative imports only work when the file is imported as part of a package. Run it as a module instead:
# Wrong
python my_package/main.py
# Correct
python -m my_package.mainMissing __init__.py
In Python 3, packages can work without __init__.py (namespace packages). But some tools and configurations still require it. If your project structure looks like this:
my_project/
├── my_package/
│ ├── module_a.py
│ └── module_b.py
└── main.pyAnd from my_package import module_a fails, add an __init__.py file to the package directory:
touch my_package/__init__.pyThe file can be empty. Its presence tells Python that the directory is a package.
PYTHONPATH
If your module is in a non-standard location, Python won’t find it unless you tell it where to look. You can add directories to the search path via the PYTHONPATH environment variable:
# Linux/macOS
export PYTHONPATH="/path/to/your/modules:$PYTHONPATH"
python app.py
# Windows (PowerShell)
$env:PYTHONPATH = "C:\path\to\your\modules;$env:PYTHONPATH"
python app.pysys.path manipulation
You can also modify the search path at runtime in your script:
import sys
sys.path.insert(0, '/path/to/your/modules')
import your_moduleThis works but is fragile. If you need this in production code, it usually means your project structure needs fixing. Prefer installing your package in development mode instead:
pip install -e .This requires a pyproject.toml (or setup.py) in your project root. It installs your project as a package so all its modules are importable from anywhere within the Python environment.
Still Not Working?
Your IDE is using the wrong Python interpreter
This is extremely common. Your terminal might use one Python while your IDE uses a different one.
VS Code: Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and search for “Python: Select Interpreter”. Choose the interpreter that has your packages installed. If you’re using a virtual environment, select the Python inside .venv/bin/python (or .venv\Scripts\python.exe on Windows).
PyCharm: Go to Settings → Project → Python Interpreter. Verify it points to the correct environment. If your venv isn’t listed, click the gear icon and add it.
pip install —user
If you can’t install packages globally (no admin/root access), use the --user flag:
pip install --user requestsThis installs the package to your user site-packages directory (e.g., ~/.local/lib/python3.11/site-packages/ on Linux). Make sure this directory is in your Python’s sys.path:
import site
print(site.getusersitepackages())Check site-packages location
If you’ve installed the package but Python still can’t find it, verify where Python is looking:
import sys
print(sys.path)And check where pip installed the package:
pip show requestsThe Location field in the output should match one of the paths in sys.path. If it doesn’t, you’re running a different Python than the one pip installed to.
Conda vs pip conflicts
If you use Conda, mixing conda install and pip install can cause issues. Conda manages its own environment, and pip might install packages to a different location than Conda expects.
Best practice:
- Install as much as possible with
conda installfirst. - Use
pip installonly for packages not available through Conda. - Never run
pip installoutside an active Conda environment.
If things are broken, recreate the environment:
conda deactivate
conda env remove -n myenv
conda create -n myenv python=3.11
conda activate myenv
conda install numpy pandas # conda first
pip install some-other-package # pip for the restDocker and containers
If you’re getting this error inside a Docker container, make sure your Dockerfile installs the dependencies:
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .A common mistake is copying the code before installing dependencies, then modifying requirements.txt without rebuilding the image. Docker’s layer caching means the RUN pip install step won’t re-run unless requirements.txt changed.
Related: If you’re hitting a similar error in Node.js, see Fix: Error Cannot find module (Node.js).
Related Articles
Fix: pip No Matching Distribution Found for <package>
How to fix the pip error 'No matching distribution found' when installing Python packages, including version conflicts, platform issues, and index problems.
Fix: ERROR: Could not build wheels / Failed building wheel (pip)
How to fix pip 'ERROR: Could not build wheels', 'Failed building wheel', 'No matching distribution found', and 'error: subprocess-exited-with-error'. Covers missing C compilers, build tools, system libraries, Python version issues, pre-built wheels, and platform-specific fixes for Linux, macOS, and Windows.
Fix: error: externally-managed-environment (pip install)
How to fix the 'error: externally-managed-environment' and 'This environment is externally managed' error when running pip install on Python 3.11+ on Ubuntu, Debian, Fedora, and macOS.
Fix: Python IndentationError – Unexpected Indent or Expected an Indented Block
How to fix Python IndentationError including unexpected indent, expected an indented block, and unindent does not match, caused by mixed tabs and spaces or incorrect nesting.