Fix: ModuleNotFoundError: No module named in Python

How to fix ModuleNotFoundError: No module named in Python: wrong interpreter, missing install, package name vs import name, shadowing files and PATH issues.

ModuleNotFoundError: No module named 'x' means the Python interpreter running your code can’t find x, and the usual cause is that you installed it into a different Python. Work through the checks below in order.

1. Find out which Python is running

python -c "import sys; print(sys.executable)"

Compare this with where you installed the package. If it is the system Python and you installed into a virtual environment (or the reverse), that is your problem.

2. Install with the same interpreter

python -m pip install requests

Using python -m pip guarantees that pip belongs to the same Python as python. A bare pip can point at a different installation. Then check it is there:

python -m pip show requests

3. Activate your virtual environment

If you use a virtual environment, activate it before running your script (source .venv/bin/activate, or .venv\Scripts\activate on Windows). With uv, run uv run python script.py and it uses the project’s environment automatically.

4. Check the import name

Some packages install under one name and import under another:

Install with Import as
Pillow PIL
scikit-learn sklearn
beautifulsoup4 bs4
PyYAML yaml
opencv-python cv2
python-dotenv dotenv

5. Look for a file that shadows the module

If you have a file called random.py, requests.py or email.py in your project, Python imports yours instead of the real one, and things break. Rename it and delete its __pycache__ folder.

6. Make your own package importable

For modules in your own project, the working directory matters. Run code from the project root with python -m package.module, rather than python package/module.py. For a longer-term fix, give the project a pyproject.toml and install it in editable mode with pip install -e .. See src layout vs flat layout and pyproject.toml explained.

7. Check your editor

VS Code and PyCharm each have a setting for the interpreter. If the terminal works but the editor’s run button doesn’t, the editor is using another Python. In VS Code, run Python: Select Interpreter. See the best Python IDEs and editors.

8. Installing was blocked

If pip install failed with an externally-managed-environment message, see how to fix that error.

For missing modules after upgrading Python, removed standard library modules are a common cause; see Python 3.12 end of life and Python 3.13 end of life. If you work on Windows, see how to install Python on Windows for py launcher tips.

Frequently Asked Questions

What causes ModuleNotFoundError: No module named?

Python cannot find the module in the interpreter that is running your code. The usual causes are that the package is not installed, it is installed in a different Python or virtual environment, the import name differs from the install name, or a file in your project shadows the module.

How do I fix No module named in Python?

Install the package with the same interpreter that runs the code: python -m pip install package-name. Check which interpreter that is with python -c “import sys; print(sys.executable)”.

Why does pip install succeed but the import still fails?

pip installed into a different Python from the one running your script. Using python -m pip install guarantees that pip and python match, and your editor may also be set to a different interpreter.

Why does the install name differ from the import name?

Some packages are imported under a different name. Pillow is imported as PIL, scikit-learn as sklearn, beautifulsoup4 as bs4, PyYAML as yaml and opencv-python as cv2.

Why is my own module not found?

Your script is probably run from a different directory, or the package lacks the right layout. Run it from the project root, use python -m package.module, or install the project in editable mode with pip install -e .