ModuleNotFoundError: No Module Named X — Interpreter and Path Mismatch

The module is probably installed — just not for the interpreter you're running, or not on sys.path. Trace which python is executing and where it looks, and the error resolves fast.

What you'll see

Root causes

Package installed for a different interpreter

pip and python can point to different installations. python -m pip install <pkg> guarantees the install goes to the interpreter you run.

sys.path doesn't include your project root

Running python app/main.py from inside app/ means the parent dir isn't importable. Print the search path: python -c "import sys; print(sys.path)".

Name shadowing

A local file named like the package (email.py, json.py, logging.py) shadows the stdlib/installed module. The import silently loads yours and explodes elsewhere.

Fix it

  1. Identify the exact interpreter running
    which python python3 && python -c "import sys; print(sys.executable)"
  2. Install with that interpreter explicitly
    python -m pip install <pkg>   # and in the right env: source ~/.venvs/proj/bin/activate first
  3. Verify importability in the same context
    python -c "import <pkg>; print(<pkg>.__version__, <pkg>.__file__)"
  4. Fix layout: run from project root or install your package
    # cd <repo-root> && python -m package.main  — or: pip install -e . so your own code is importable anywhere

Field note

cron/systemd jobs run with different PATH and no activated venv — point them at the venv's absolute python path. docker: installing with a different user or --user flag than the runtime user produces exactly this error.

Common questions

pip says 'already satisfied' but Python can't import it. Why?

Two Pythons. The pip you ran belongs to another interpreter or env. Use python -m pip (same interpreter as the failing script) and check sys.executable to confirm.

Why does my import fail only when run under cron/systemd?

Those environments don't activate your venv and start with a minimal PATH. Use absolute paths: /home/you/.venvs/proj/bin/python script.py in the service/cron definition.

Ship it right the first time

Our most-documented failures, packaged as ready-to-ship starter kits: Docker, Kubernetes, and Terraform.

Browse the template store →

One-time. Yours to modify. Instant download from the NinjaOps template store.