Python Top Python Error

How to Fix 'ModuleNotFoundError: No module named' in Python

Error Message
ModuleNotFoundError: No module named 'requests' ModuleNotFoundError: No module named 'django'
Quick Fix
python3 -m pip install <module_name> && python3 script.py

Why Does This Happen?

The package is installed in a different Python environment than the one running your script, or VS Code is pointing to the global interpreter instead of your virtual environment.

Step-by-Step Solution

1
Step 1: Install with matching python3 -m pip
Terminal / Code
python3 -m pip install requests
Using python3 -m pip ensures the package goes into the exact Python running your script.
2
Step 2: Activate your virtual environment
If your project uses a venv, activate it first:
Terminal / Code
source venv/bin/activate
pip install requests
On Windows PowerShell: .\venv\Scripts\Activate.ps1
3
Step 3: Fix VS Code Python interpreter
Press Ctrl+Shift+P (Cmd+Shift+P on Mac), type 'Python: Select Interpreter', and choose your venv.
Terminal / Code
# Ctrl+Shift+P > Python: Select Interpreter
# Select: ./venv/bin/python
VS Code will re-index all packages in the virtualenv.

Common Mistakes to Avoid

Naming your script the same as the package (e.g. requests.py or random.py) — Python imports your file instead of the library!
Installing the package with pip but running the script with python3 (two different environments).

Frequently Asked Questions

Why does pip say 'Requirement already satisfied' but the error still appears?
pip installed into Python A but your script runs with Python B. Use python3 -m pip install to guarantee alignment.

Learn to Write Code That Doesn't Break

DoxaTech Hub's 10-month programme teaches you Git internals, Linux systems, C, Python, SQL, Docker, and React from first principles — so you spend less time fixing errors and more time shipping.

Start Learning →