← Back

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 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

  1. 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 Pillow vs import PIL.)
  2. Install with the same interpreter you run with: python -m pip install <name>.
  3. If it was already installed, ask that interpreter what it sees: python -m pip list — and compare with python -c "import sys; print(sys.executable)" to confirm you are running the Python you think you are.
  4. 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.
  5. 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

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 ले लेगा।

पूरी Hindi explanation पढ़ें →