ModuleNotFoundError: No module named … — the Python fix

This is one of the most searched Python errors, and it almost never means what beginners think. Python isn’t saying the package doesn’t exist — it’s saying the specific Python running your script can’t see it. Nine times out of ten, the package was installed into a different Python (or a different virtual environment) than the one executing the code.

The fix, in order

  1. Install with the same Python that runs the script: python -m pip install packagename. Using python -m pip instead of plain pip removes this whole class of error, because plain pip can belong to a different Python.
  2. Confirm which Python is running: python -c "import sys; print(sys.executable)" — and compare it with where pip put the package: python -m pip show packagename (look at the Location line). Different folders = your answer.
  3. If you use a virtual environment, activate it before running (Windows: .venv\Scripts\activate; Mac/Linux: source .venv/bin/activate), then install and run inside it. In VS Code, also check the interpreter shown in the bottom bar matches.
  4. In Jupyter, install into the notebook’s kernel: run a cell with %pip install packagename, then restart the kernel. Installing in a terminal does nothing for a notebook running a different environment.
  5. Check your own file names. A file of yours named like a library (random.py, requests.py, email.py) shadows the real one. Rename it and delete the __pycache__ folder beside it.
  6. Check the import name vs the install name. Some packages install under one name and import under another: install Pillow, import PIL; install beautifulsoup4, import bs4; install scikit-learn, import sklearn.

If it still happens

If the module is your own file, Python only finds it if its folder is on the search path — running the script from the project root, or running it as a module (`python -m package.module`), fixes most cases. Printing `sys.path` in the failing script shows exactly which folders Python is searching.

Common questions

I installed it and Python still says no module. What gives? Almost always two Pythons: pip installed into one, your editor/terminal runs the other. Step 1 (python -m pip) plus step 2 (comparing locations) settles it in a minute.

Does reinstalling Python help? It’s the last resort, not the first. A reinstall can clear a genuinely broken setup, but most cases are an environment mismatch that a reinstall recreates just as easily.

Related errors

Node: Cannot find module · ‘pip’ is not recognized · TypeError: ‘NoneType’ object is not subscriptable