Home › Errors › ModuleNotFoundError
ModuleNotFoundError: No module named 'requests' — decoded
Exception: ModuleNotFoundErrorCategory: import timeRaised at: at the import line, before anything runs
What this message means
An import asks Python to find a module by name. Python searches a fixed list of places — built-ins, the script's own folder, installed packages — and if the name is nowhere, the import fails before a single line of your program runs. ModuleNotFoundError names the module it could not find.
Because the failure happens at import time, the fix is almost never inside your code. It is in your environment: install the package, correct the name, or run from the right place.
Traceback (most recent call last):
File "main.py", line 1, in <module>
import requests
^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'requests'
The four causes, and their fixes
- The package is not installed in this environment. Fix:
pip install requests— and if you have several Python versions,python -m pip install requestsguarantees the same interpreter you run with. - The name is misspelled or the import is wrong.
import pandafails because the package ispandas;import random.randintfails because only the module can be imported —from random import randintis the correct spelling. - You are in the wrong environment. The package is installed in your venv, but you ran the script with the system Python (or the other way round). Activate the environment you installed into.
- Your own file shadows or hides the module. A file named
random.pyin the current folder makesimport randomimport *your* file; a script run from inside a package folder cannot find its sibling by the package name. Rename the file, or run from the parent directory.
# The wrong-vs-right spellings that cause most cases
import panda # ModuleNotFoundError — the package is "pandas"
import pandas # right
import random.randint # ModuleNotFoundError — you cannot import an attribute
from random import randint # right
The fix, step by step
- Copy the exact module name from the message — including case — and check it is spelled the way the package spells it. (Import name and install name sometimes differ:
pip install Pillowvsimport PIL.) - Install with the same interpreter you run with:
python -m pip install <name>. - If it was already installed, ask that interpreter what it sees:
python -m pip list— and compare withpython -c "import sys; print(sys.executable)"to confirm you are running the Python you think you are. - Still failing? Look in the current folder for a file whose name collides with the module, and check you are running from the directory your project expects.
- In a notebook or in-browser environment, follow that environment's install command (in PyDebug's editor, the common data packages are preloaded — the import works with no install at all).
Why the error happens before your code runs
Imports execute immediately, top to bottom, the moment the file loads — before main(), before your first print. That is why this traceback shows line 1 even when line 1 looks fine, and why "but my other imports worked" tells you nothing: Python stops at the *first* name it cannot find. It is also why this error and NameError are cousins — one means "module not found", the other means "name not found", and both are resolved outside the logic of the program itself.
Related pages
- ModuleNotFoundError (Hindi) — the same four causes explained in Hindi.
- NameError: name is not defined — the sibling failure when a missing import surfaces as a missing name.
- The complete Python errors guide — every exception the practice catalog trains, on one page.
Fix it interactively
A missing module is an environment problem, but the habits around it are code problems. These free problems practice the name-resolution bugs that live next door:
Practice more Python bugs →🇮🇳 Hindi में समझें
import लिखते ही Python उस module को ढूंढता है — built-ins, script वाला folder, और installed packages। न मिले तो program की एक भी line चले बिना ModuleNotFoundError। चार कारण: (1) package install नहीं है → python -m pip install <name> करो, (2) नाम की spelling गलत है → pandas नहीं panda, और import random.randint नहीं — from random import randint, (3) गलत environment में install है → जिस Python से चला रहे हो उसी में install करो, (4) आपकी अपनी file का नाम collision कर रहा है → folder में random.py मौजूद है तो import random आपकी ही file ले लेगा।