How to fix "ModuleNotFoundError: No module named" in Python

Tested: we reproduced this error and confirmed the fix on Ubuntu 24.04 with Python 3.12 and a venv, November 29, 2026.

Python looked for the module in the interpreter you ran and didn’t find it. Most often the package is installed, just into a different Python. Check which one runs your code, then install with that same interpreter:

python -c "import sys; print(sys.executable)"
python -m pip install requests

We reproduced this on Python 3.12: requests installed in a virtual environment, and the script run with the system python3. The system Python raised ModuleNotFoundError: No module named 'requests'; the same script run with the environment’s Python printed the version.

Find your cause

Sign Cause Fix
pip install said it worked Installed into another Python Install with python -m pip using the interpreter from sys.executable
Works in the terminal, fails in the editor Editor uses another interpreter Select .venv/bin/python in the editor
You installed beautifulsoup4 and import beautifulsoup4 The import name differs from the package name import bs4 (likewise pyyaml is yaml, pillow is PIL, scikit-learn is sklearn)
A misspelled name in the error Typo Fix the import
No module named 'yourproject' when running a file inside it Running a file instead of the package Run from the project root with python -m yourproject.main
It worked yesterday The environment isn’t activated in this terminal source .venv/bin/activate
Works in the terminal, fails in Jupyter The notebook kernel is another Python Run %pip install package in a notebook cell, which installs into the kernel’s Python

We checked the import-name row too: after installing beautifulsoup4 and pyyaml, import beautifulsoup4 failed and import bs4 and import yaml worked.

Fix: install into the Python you actually run

which python3            # Mac and Linux; on Windows: where python
python3 -m pip --version

Both should point to the same place. If they don’t, always use the -m form:

python3 -m pip install requests

Inside a virtual environment, activate it first so python and pip both point there. Installing into the system Python on recent Linux is blocked with externally-managed-environment; see our guide to that error.

Fix: imports inside your own project

Running a file inside a package directly breaks imports between its modules. We got ImportError: attempted relative import with no known parent package from python pkg/main.py, and the same code worked with:

python -m pkg.main

Run from the folder that contains pkg/, and give each package folder an __init__.py file.

Setting PYTHONPATH

You can also tell Python where to look by adding a folder to PYTHONPATH, for example export PYTHONPATH="$PWD/src" before running your script. In our test a script that imported a package from src/ failed with ModuleNotFoundError and worked with that variable set. It works, but it lives in your shell rather than the project, so it breaks again in a new terminal, in your editor and in CI. Running with python -m from the project root, or installing your project with python -m pip install -e ., keeps working everywhere.

Confirm it worked

python -c "import requests; print(requests.__file__)" prints a path inside the environment you expect. Replace requests with your package’s import name.

FAQ

Why does pip say the package is installed but import fails?

pip installed it into a different Python than the one running your code. Run python -c "import sys; print(sys.executable)" and install with that exact interpreter: /path/to/python -m pip install package.

Why does my editor show the error when the terminal works?

VS Code, PyCharm and Jupyter each pick an interpreter. Point them at your project's .venv/bin/python (VS Code: Python: Select Interpreter).

What's the difference between ModuleNotFoundError and ImportError?

ModuleNotFoundError is a kind of ImportError raised when the module itself can't be found. A plain ImportError means the module was found but something inside it failed, such as cannot import name or a relative import with no parent package.