ModuleNotFoundError: No module named ‘x’

Python
Traceback (most recent call last):
  File "shelf.py", line 1, in <module>
    import pandas_fake
ModuleNotFoundError: No module named 'pandas_fake'

What Python is actually saying

You asked me to load a toolkit by that name. I looked everywhere I know to look, and it isn’t there.

Python ships with a set of built-in modules — math, random, datetime, json and others. Everything else has to be installed onto your computer before you can import it.

Cause 1: it was never installed

Most of the well-known libraries — requests, pandas, numpy, matplotlib — do not come with Python. Install from your terminal, not inside your Python file:

Python
pip install requests

Then import requests works.

pip install puts it on your computer. import brings it into this program. You install once; you import in every file that needs it.

Cause 2: the install name and the import name differ

This one is genuinely confusing and nobody warns you:

You installYou import
pip install beautifulsoup4import bs4
pip install Pillowimport PIL
pip install scikit-learnimport sklearn
pip install opencv-pythonimport cv2

If pip says the package is already installed and Python still can’t find it, check the library’s own documentation for the import name.

Cause 3: a typo

Python
import requests

Same as a NameError — read the name in the error against what you typed.

Cause 4: installed into a different Python

The awkward one. If you have more than one Python on your machine, or you’re using a virtual environment, pip may have installed into one Python while your editor runs another.

Check they match:

Python
python3 -m pip install requests

Using python3 -m pip rather than plain pip installs into the Python you’re actually running, which resolves most of these.

In VS Code, check the interpreter shown in the bottom-right corner is the one you installed into.

What to check, in order

  1. Is it a built-in module, or does it need installing?
  2. Have you run pip install for it?
  3. Does the install name differ from the import name?
  4. Is the spelling right?
  5. Try python3 -m pip install instead of pip install

Remember this

This error is rarely about your code. It’s about your setup — which means it’s fixable in the terminal, not the editor, and it usually stays fixed.


The Python Troubleshooting Handbook covers thirty errors like this one — 77 pages, offline, no searching required.