Python ImportError: No Module Named Requests

Python ImportError: No Module Named Requests

  1. Install requests Module to Fix ImportError: No module named requests in Python
  2. Install requests Module in Anaconda Environment
  3. Update PYTHONPATH to Fix ImportError: 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.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

Related Article - Python ImportError

  • ImportError: No Module Named mysql.connector
  • ImportError: Missing Required Dependencies Numpy
  • ImportError: No Module Named Sklearn in Python
  • Related Article - Python Error

  • Python PermissionError: [WinError 5] Access Is Denied
  • Python TypeError: 'DataFrame' Object Is Not Callable
  • Python TypeError: Can't Convert 'List' Object to STR
  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10