How to Resolve "ModuleNotFoundError: No module named 'requests'" in Python
The ModuleNotFoundError: No module named 'requests'
is one of the most common errors encountered by Python developers, especially those new to making HTTP requests. It signals that the Python interpreter can not find the requests
library when your script tries to import it. This almost always means the package hasn't been installed correctly in the specific Python environment you are using. This guide provides clear, step-by-step solutions to install requests
properly and resolve the error.
Understanding the Error
This error means that when Python executes import requests
or from requests import ...
, it searches its list of installed packages and known locations but can not find a module named requests
.
Solution 1: Install requests
using pip
(Most Common)
The requests
library is a third-party package and needs to be installed using pip
, Python's package installer.
Open your terminal or command prompt and run:
pip install requests
Variations (Choose the one appropriate for your setup):
# If you primarily use Python 3
pip3 install requests
# If pip/pip3 are not in your system PATH
python -m pip install requests
python3 -m pip install requests
# On Windows, 'py' launcher might be available
py -m pip install requests
Troubleshooting Common Installation Problems:
If pip install requests
gives an error or the ModuleNotFoundError
persists, check these points:
- Using the Correct
pip
: Ensure you're using thepip
linked to the Python interpreter running your script (usuallypip3
orpython3 -m pip
for Python 3). Verify versions withpython --version
andpip --version
(orpip3 --version
). - Working with Virtual Environments (Crucial!): This is the most frequent reason for the error. You might have installed
requests
globally but are running your script inside a virtual environment (or vice-versa).- Always activate your virtual environment before installing:
# Create (if needed)
python3 -m venv venv
# Activate (adjust for your OS)
source venv/bin/activate # Linux/macOS
.\venv\Scripts\Activate.ps1 # Windows PowerShell
.\venv\Scripts\activate.bat # Windows CMD
# Install INSIDE the active environment
pip install requests - Confirm your IDE (VS Code, PyCharm) uses the Python interpreter from your activated virtual environment.
- Always activate your virtual environment before installing:
- Permissions Issues (
--user
orsudo
): When installing globally (not recommended).- Recommended: Install for the current user:
pip install requests --user
- Use Cautiously: Use admin/root privileges:
sudo pip install requests
(Linux/macOS) or Run Terminal as Administrator (Windows). Virtual environments avoid this need.
- Recommended: Install for the current user:
- Multiple Python Installations / IDE Configuration: Double-check that the Python interpreter selected in your IDE matches the one you used
pip
with. Use the "Select Interpreter" feature in your IDE. - Naming Conflicts (
requests.py
): Ensure none of your project files are namedrequests.py
. This "shadows" the installed package. Rename your file if necessary. Also, avoid usingrequests
as a variable name before the import.
Verifying Installation (pip show
)
Confirm the package is installed in the active environment:
pip show requests
# Or variants like: pip3 show requests / python3 -m pip show requests
This command should display package details (Name, Version, Location, etc.). "Package(s) not found" means it's not installed in that specific environment.
Platform/Tool Specific Installation Notes:
- Windows: Use CMD or PowerShell.
py -m pip install requests
is often reliable. Run as Administrator for global installs if needed (prefer venvs/--user
). - macOS / Linux: Use the Terminal. Use
pip3
orpython3 -m pip
. Usesudo
cautiously for global installs. - VS Code: Use the integrated terminal (
Ctrl + ``
). Ensure the correct Python interpreter (often venv) is selected. - PyCharm: Use the Terminal (
Alt+F12
) or the Python Packages tool (File > Settings > Project > Python Interpreter >+
). Ensure the correct project interpreter is set. - Anaconda: Use the Anaconda Prompt/Navigator or terminal in an active conda environment. Preferred command:
conda install -c anaconda requests
# Or sometimes just: conda install requests
# Pip also works within conda environments:
pip install requests - Jupyter Notebook: Install from a notebook cell:
Restart the kernel after installation.
!pip install requests
# Or for user install:
# !pip install requests --user
Using requests
After Installation
Once installed correctly, you can import and use the library:
import requests
try:
res = requests.get('https://httpbin.org/get', timeout=10)
res.raise_for_status() # Good practice to check for HTTP errors
data = res.json()
print(data)
except ImportError:
print("Error: 'requests' module not found.")
print("Please install it using: pip install requests")
except requests.exceptions.RequestException as e:
print(f"An error occurred during the request: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
Related IDE Error: "Import "requests" could not be resolved..."
Linters like Pylance (in VS Code) might show this warning even if the package is installed. This usually means:
- Incorrect Interpreter Selected: The IDE isn't looking at the Python environment where
requests
was installed. Use the "Python: Select Interpreter" command (Ctrl+Shift+P) in VS Code to choose the correct one (especially the one inside your virtual environment). - IDE Needs Restart: Sometimes, restarting VS Code or PyCharm resolves the issue after installing a package.
- Linter Glitch (Workaround): As a last resort, if you're sure the package is installed correctly and your code runs, you can suppress the Pylance warning for that line:
import requests # type: ignore
warningOnly use
# type: ignore
if you are certain the installation is correct and the problem lies with the linter. It hides potential real import errors.
Conclusion
The ModuleNotFoundError: No module named 'requests'
almost always signifies that the requests
package needs installation in your active Python environment.
- The standard solution is
pip install requests
. - Ensure you're using the correct
pip
command and, most importantly, that you are working within the intended virtual environment. - Verifying your IDE's interpreter setting is key to resolving both the runtime error and related linter warnings.