How To Resolve NameError: Name ‘requests’ Is Not Defined In Python

NameError: name 'requests' is not defined in Python

NameError: name ‘requests’ is not defined in Python. Why does this error happen, and how to fix it? The following article will help you answer this questions. Let’s read it now.

What causes the NameError: name ‘requests’ is not defined in Python?

The requests module makes it possible to send HTTP requests in Python. The HTTP request will return a ‘response object’ with response data. 

‘NameError’ happens when you use a variable, function, or module that is not declared or you are not using it properly. And the NameError: name ‘requests’ is not defined occurs because you don’t import the requests module before using it.

Example:

# The get() method sends a get request to the url you specify
res = requests.get('https://learnshareit.com/about-us/')

# The url method returns the url of the response
print(res.url)

Output:

Traceback (most recent call last):
File "d:\vscode\Python\main.py", line 2, in <module>
    res = requests.get('https://learnshareit.com/about-us/')
          ^^^^^^^^
NameError: name 'requests' is not defined

How to solve the NameError: name ‘requests’ is not defined in Python?

Import the requests module

Example:

  • Import the requests module before using it.
import requests

# The get() method sends a get request to the url you specify
res = requests.get('https://learnshareit.com/about-us/')

# the url method returns the url of the response
print(res.url)

Output:

https://learnshareit.com/about-us/

Install the requests module

Windows

You must first have Python and pip installed on your computer, preferably the most recent versions.

Open a command prompt and enter the following command to install requests using pip:

pip3 install requests

The requested library was successfully downloaded.

Linux

On the majority of Linux distributions, Python is preinstalled. However, pip installation is required. From the terminal, Pip can be installed. The syntax for installing pip varies between Linux versions, though. You must have root access to install pip. After that, open a terminal and enter the commands using the right syntax for your Linux distribution.

The installation syntax for pip in specific well-known Linux systems is as follows:

For Debian, Linux Mint, and Ubuntu:

sudo apt install python-pip3

For Fedora, CentOS 8 (and newer), and Red Hat:

sudo dnf install python-pip3

For Older versions of Red Hat and CentOS 6 and 7: 

sudo yum install epel-release
sudo yum install python-pip3

For Manjaro and Arch Linux:

sudo pacman -S python-pip

For OpenSUSE:

sudo zypper python3-pip

And the last step is to install the requests module:

pip3 install requests

macOS

You install the requests module by following these steps:

python3 get-pip.py
pip3 install requests

Summary

Hopefully, through the article, you could handle the error NameError: name ‘requests’ is not defined in Python. Remember to import the module before using it and how to install it if you still need to learn how to install it. Thank you for reading the article.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *