← Back to Error Lab

ModuleNotFoundError: No module named 'requests'

Published 2026-04-18

Educational use only. Content explains errors and defensive fixes for systems you own or are authorised to test. Do not use any technique here to access data, accounts, or networks without permission.

Root Cause

This straightforward error means Python cannot find the package you are trying to import. It happens when the package has not been installed via pip, or more confusingly, when it is installed in a different Python environment than the one executing the script. When working with multiple projects, global installations can conflict. If you use a virtual environment but forget to activate it, the script runs using the global Python interpreter, which lacks the project-specific dependencies.

Fix / Solution

First, install the package using `pip install <package_name>`. The best practice is to always use Python virtual environments (`venv` or `conda`) for projects to isolate dependencies. Ensure your IDE is configured to use the interpreter from your active virtual environment. If using a `requirements.txt` file, run `pip install -r requirements.txt`.

Code Snippet

# ❌ Script fails because 'requests' is not installed in current env
import requests

# ✅ Create and activate a virtual environment (Linux/macOS)
python3 -m venv venv
source venv/bin/activate

# ✅ Install the package
pip install requests

# ✅ Run the script
python script.py