How to Fix Python ImportError: No Module Named Requests
-
Install
requests
Module to FixImportError: No module named requests
in Python -
Install
requests
Module in Anaconda Environment -
Update
PYTHONPATH
to FixImportError: No module named requests
in Python
An ImportError
is raised when a specified module or a member of a module cannot be imported. This error occurs (in most cases) if the module you are trying to import is not installed.
Sometimes having different versions of Python might also cause the problem. For example, you are using Python 3, but the installed module is for Python 2.
The error ImportError: No module named requests
means the Python interpreter cannot import the requests
module.
This tutorial shows ways to fix ImportError: No module named requests
in Python.
Install requests
Module to Fix ImportError: No module named requests
in Python
The module requests
import might not be installed on the Python environment. As a result, you get ImportError: No module named requests
.
You can solve this error by installing the requests
module.
Run the command below in the terminal.
python -m pip install requests
For Python 3, use the following command.
python3 -m pip install requests
You can also use the system package manager tool in Linux to install requests
.
For CentOS/RHEL:
sudo dnf install python-requests
For Ubuntu/Debian:
sudo apt-get install python3-requests
Install requests
Module in Anaconda Environment
If you use Anaconda as your Python package manager, installing requests
with pip
might not solve the problem.
You can run this command to install requests
in Anaconda.
conda install -c anaconda requests
Once installed, you can import the requests
module into your Python program.
The following example requests a URL and prints the status code.
import requests
x = requests.get("https://www.delftstack.com")
print(x)
Output:
<Response [200]>
The response is successful.
Update PYTHONPATH
to Fix ImportError: No module named requests
in Python
If the error is not solved after installing the requests
module, it might be caused due to the incorrect path configuration in the PYTHONPATH
.
For instance, the module requests
is installed, but its path is not listed in the environment variable. As a result, the Python program is unable to locate the module.
The following example displays all paths used by Python for importing modules.
import sys
print(sys.path)
If the directory where the requests
module is installed is not in the output, you can use the sys.path.append
method to add a new directory path.
import sys
sys.path.append(
r"C:\Users\rhntm\AppData\Local\Programs\Python\Python310\Lib\site-packages"
)
The ImportError
is thrown when the Python program cannot locate the specified module. You can solve this error by installing the module with Python’s package manager pip
.
Now you know the reasons for ImportError
and how to fix that error in Python. We hope you find these solutions helpful.
Related Article - Python ImportError
- How to Fix ImportError: No Module Named mysql.connector
- How to Fix ImportError: Missing Required Dependencies Numpy
- How to Fix ImportError: No Module Named Sklearn in Python
Related Article - Python Error
- Can Only Concatenate List (Not Int) to List in Python
- How to Fix Value Error Need More Than One Value to Unpack in Python
- How to Fix ValueError Arrays Must All Be the Same Length in Python
- Invalid Syntax in Python
- How to Fix the TypeError: Object of Type 'Int64' Is Not JSON Serializable
- How to Fix the TypeError: 'float' Object Cannot Be Interpreted as an Integer in Python