Blog

Blog · Error Fixes

How to Fix Python "ModuleNotFoundError: No module named"

ModuleNotFoundError means Python searched every path and never found the module — usually the wrong interpreter or venv, or an install-name that differs from the import-name. Here is the fix.

XCODX Team · 3 min read

ModuleNotFoundError: No module named 'x' means Python searched every entry on sys.path and never found a module or package with that name — so this is fundamentally about the interpreter or environment not having the package where *this specific Python* is looking. (ImportError: cannot import name … is different: the module was found, but a name inside it could not be pulled out.)

The 6 common causes and their fixes

1. The package is not installed for the active interpreter

Install into the exact Python running the code with python -m pip.

python -m pip install requests
# `python -m pip` guarantees pip targets the same interpreter as `python`

2. The wrong virtual environment or interpreter

The package is installed, but into a different venv or the system Python — very common in IDEs and Jupyter.

python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
python -m pip install requests
python -c "import sys; print(sys.executable)"   # confirm which python

3. The install name is not the import name

The PyPI distribution name often differs from the importable module name.

pip install beautifulsoup4   ->  import bs4
pip install Pillow           ->  from PIL import Image
pip install PyYAML           ->  import yaml
pip install scikit-learn     ->  import sklearn
pip install opencv-python    ->  import cv2
pip install python-dateutil  ->  import dateutil

4. Wrong directory, sys.path, or a shadowing file

A local file that shares a module’s name (e.g. your own random.py or tokenize.py) shadows the real one. Rename it, remove stray .pyc, and run from the project root as a module.

# a file named tokenize.py in the cwd breaks `import tokenize`
# ✅ rename the local file, then run as a module
python -m mypackage.mymodule

5. python2 vs python3 / pip vs pip3

pip may point at a different install than python3.

python3 -m pip install requests
python3 app.py

6. Missing package structure or a bad relative import

Running a file directly that uses a relative import raises attempted relative import with no known parent package.

# mypkg/mod.py contains: from . import helper
# ❌ python mypkg/mod.py
# ✅ run as a module so the parent package is known
python -m mypkg.mod

Quick diagnostic checklist

  1. python -c "import sys; print(sys.executable)" — confirm which interpreter is actually running.
  2. python -m pip show <pkg> — is it installed for THAT interpreter? If not, python -m pip install <pkg>.
  3. Check install-name vs import-name (beautifulsoup4→bs4, Pillow→PIL, opencv-python→cv2).
  4. Ensure no local file shadows the module, and launch from the project root (python -m pkg.mod).
  5. If it is cannot import name …, that is an ImportError — look for a typo or a circular import, not a missing install.

Frequently asked questions

Why does pip install succeed but import still fails?
pip installed into a different interpreter than the one running your code. Use python -m pip install <pkg> and confirm with python -c "import sys; print(sys.executable)" and python -m pip show <pkg> that both point to the same Python.
I installed beautifulsoup4 but "import beautifulsoup4" fails — why?
The install name is not the import name. Install beautifulsoup4, import bs4. Other common ones: Pillow→PIL, PyYAML→yaml, scikit-learn→sklearn, opencv-python→cv2, python-dateutil→dateutil.
What is the difference between ModuleNotFoundError and ImportError?
ModuleNotFoundError (a subclass of ImportError) means the module itself was not found. ImportError also covers "found the module, but a name inside it failed" — usually a typo’d symbol or a circular import, which needs a different fix than installing a package.

Run Python instantly — no local install, no venv confusion — in the XCODX online compiler. It executes Python in the browser so you can confirm a fix without touching your machine’s interpreters.