ModuleNotFoundError 🐍 Python

ModuleNotFoundError: No module named 'x'

Python can’t find the module you import — it isn’t installed in the interpreter/virtualenv that’s running, or the import path is wrong.

Seen on: Python Docker

Meaning

Most of the time the package *is* installed — just into a different Python. pip and python can point to different interpreters, and IDEs/cron/systemd may not use your virtualenv.

Common causes

  • Package not installed in the active environment
  • pip belongs to a different Python than python (use python -m pip)
  • Virtualenv not activated (IDE, cron, Docker, systemd)
  • Import name differs from package name (pip install opencv-python → import cv2, beautifulsoup4 → bs4, Pillow → PIL)
  • Running a file inside a package directly instead of python -m package.module
  • Local file shadows the module (a file named requests.py)

⚡ Quick fix

  1. Install with the same interpreter: python -m pip install <package>
  2. Check which python / python -c "import sys; print(sys.executable)"
  3. Activate the virtualenv or select it in your IDE
  4. Rename local files that shadow library names

Detailed fix by platform

Python

  1. Make sure pip and python match:
    bash
    python -c "import sys; print(sys.executable)"
    python -m pip install requests
    python -m pip show requests   # Location: should be inside your venv

Docker

  1. Install requirements in the image (RUN pip install -r requirements.txt) and use the same Python in CMD.

How to diagnose

  1. Interpreter — Which Python runs the code?
  2. Installed — Is the package in that interpreter?
  3. Name — Import name vs pip name?
  4. Shadowing — Local file with the same name?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.