How to Fix ModuleNotFoundError: No module named ‘requests’ in Python

A Professional Guide to Python Environment Management

Encountering a ModuleNotFoundError is a standard part of the Python learning curve. While it might seem like a simple installation failure, this error usually signals a deeper disconnect between your development environment and your Python interpreter.

ModuleNotFoundError: No module named ‘requests’ error in Python

1. Why the Error Occurs (The Paradox)

The most common confusion arises when pip install requests confirms success, yet the script still fails. This typically happens due to Version Mismatching.

  • The Cause: You might be installing the library for Python 2.7 while running Python 3.x, or vice versa.
  • The Diagnostic: Check which version of Python is linked to your pip command by running:
    • pip --version
    • pip3 --version

Note: If your terminal uses Python 3.12 but your IDE is set to 3.10, the “Requirement already satisfied” message in your terminal won’t help your script run.

2. OS-Specific Installation Commands

The method for installing Requests varies depending on your operating system and environment configuration.

Operating SystemRecommended Command
Windowspy -m pip install requests
macOS / Linuxpython3 -m pip install requests
Debian / Ubuntusudo apt-get install python3-requests
macOS (Homebrew)brew install python

3. Resolving IDE-Specific Issues

Many developers find that code runs in the terminal but fails inside an Integrated Development Environment (IDE).

VS Code

This is almost always due to the wrong Python Interpreter being selected.

  • Fix: Check the bottom-right corner of the status bar. Ensure the selected interpreter matches the path where you installed the Requests module.

PyCharm

PyCharm handles packages in a more structured way.

  • Best Practice: Check the “Inherit global site-packages” box during project creation.
  • Manual Fix: Go to Settings > Project > Python Interpreter and add the requests package using the “+” icon.

4. The Gold Standard: Virtual Environments (venv)

The most robust way to avoid environment errors is to stop installing packages globally. A Virtual Environment creates an isolated “bubble” for your project.

Setup Workflow:

  1. Create: python -m venv venv
  2. Activate (Windows): .\venv\Scripts\activate
  3. Activate (Unix/macOS): source venv/bin/activate
  4. Install: pip install requests

This ensures your dependencies are locked to that specific project folder.

5. Subtle Errors: Typos and Shadowing

Sometimes the logic is perfect, but the naming is not.

  • File Shadowing: If you name your script requests.py, Python will try to import your file instead of the library. Never name your files after the libraries you are importing.
  • The S-Factor: The library is named requests (plural). Attempting to import request will fail every time.

6. Advanced Troubleshooting

When I cannot change environment settings (e.g., on a restricted server), I usually use the manual path approach:

import sys
# Check where Python is looking for modules
print(sys.path)

# Temporary fix: manually append the installation path
sys.path.append('path/to/site-packages')
import requests

Tip: While path appending works as a “quick fix,” staying consistent with pip3 and virtual environments is the definitive way to keep your workflow smooth.

FAQ

The Root Cause: You probably installed the library before activating the environment or forgot to activate it in your current terminal session. If you don't see (venv) at the start of your command line, you are still installing to the global system, leaving your project "bubble" empty.

The Strategic Fix: Activate the environment first (source venv/bin/activate or .\venv\Scripts\activate) and then run the install command.

The Root Cause: This is a pathing mismatch. IDEs often default to their own internal environments or a different "Python Interpreter" than the one your system terminal uses.

The Strategic Fix: In VS Code, click the Python version in the bottom-right status bar and select the interpreter that matches your installation path. In PyCharm, ensure your "Project Interpreter" settings point to the correct venv.

The Root Cause: By default, Python tries to install packages in system folders that require Admin or Sudo rights. Without these, the installation fails and the module remains missing.

The Strategic Fix: Use the user-level flag:

pip install --user requests

This bypasses system restrictions by installing the library only for your local user profile, resolving the error immediately without needing elevated privileges.

Leave a Comment