NameError: name 'requests' is not defined in Python [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Table of Contents

  1. NameError: name 'requests' is not defined in Python
  2. NameError: name 'request' is not defined in Python

# NameError: name 'requests' is not defined in Python

The Python "NameError: name 'requests' is not defined" occurs when we use the requests module without importing it first.

To solve the error, install the module and import it (import requests) before using it.

nameerror name requests is not defined

Open your terminal in your project's root directory and install the requests module.

shell
# ๐Ÿ‘‡๏ธ In a virtual environment or using Python 2 pip install requests # ๐Ÿ‘‡๏ธ For python 3 (could also be pip3.10 depending on your version) pip3 install requests # ๐Ÿ‘‡๏ธ If you get a permissions error sudo pip3 install requests # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install requests # ๐Ÿ‘‡๏ธ For python 3 (could also be pip3.10 depending on your version) python3 -m pip install requests # ๐Ÿ‘‡๏ธ Alternative for Ubuntu/Debian sudo apt-get install python3-requests # ๐Ÿ‘‡๏ธ Alternative for CentOS sudo yum install python-requests # ๐Ÿ‘‡๏ธ For Anaconda conda install -c anaconda requests

# Import the requests module before using it

After you install the requests module, make sure to import it before using it.

main.py
# ๐Ÿ‘‡๏ธ import requests import requests def make_request(): res = requests.get('https://reqres.in/api/users', timeout=10) print(res.json()) make_request()

import requests module before using it

The example shows how to make a GET request to a remote API using the requests module.

The res variable is a Response object that allows us to access information from the HTTP response.

Here are some of the most commonly used properties on the Response object.

main.py
import requests def make_request(): res = requests.get('https://reqres.in/api/users', timeout=10) print(res.headers) # access response headers print(res.text) # parse JSON response to native Python string print(res.json()) # parse JSON response to native Python object make_request()

You can use the same approach to make a POST request.

main.py
import requests def make_request(): res = requests.post( 'https://reqres.in/api/users', data={'name': 'Bobby Hadz', 'job': 'programmer'} ) print(res.json()) # parse JSON response to native Python object make_request()

The same approach can be used to make a PUT, DELETE, HEAD or OPTIONS requests.

If you need to send a request body, set the data keyword argument as we did in the example above.

# Make sure to not import the requests module in a nested scope

Make sure you haven't imported requests in a nested scope, e.g. a function.

main.py
def make_request(): import requests res = requests.get('https://reqres.in/api/users') # โ›”๏ธ NameError: name 'requests' is not defined res = requests.get('https://reqres.in/api/users')

importing requests module in nested scope

We imported the requests module in a function, so we aren't able to use it outside of the function.

Import the module at the top level to be able to use it throughout your code.

main.py
# โœ… Moved import statement to the top of the file import requests def make_request(): res = requests.get('https://reqres.in/api/users') res = requests.get('https://reqres.in/api/users')

The import statement for the requests module has to come at the top of the file before any code that makes use of it.

# Make sure to not import the requests module in a try/except statement

You also should be importing the requests module in a try/except statement.

main.py
try: # ๐Ÿ‘‰๏ธ Code here could raise an error import requests res = requests.get('https://reqres.in/api/users') except ImportError: res = requests.get('https://reqres.in/api/users') res = requests.get('https://reqres.in/api/users')

The code sample works, however, if the code in the try statement raises an error, the requests module won't get imported successfully.

This would cause the error because we are trying to access properties on the requests module in the outer scope and the except block.

Instead, move the import statement to the top of the file.

# NameError: name 'request' is not defined in Python

The Python "NameError: name 'request' is not defined" occurs when we use the request object in Flask without importing it first.

To solve the error import request before using it - from flask import request.

main.py
# ๐Ÿ‘‡๏ธ import request from flask import Flask, request app = Flask(__name__) @app.route("/") def hello_world(): # ๐Ÿ‘‡๏ธ Can use request here print(request.method) return "<p>Hello, World!</p>"

We had to import request from flask in order to use it.

Note that you should only use the request global object in your request handler functions because the request object is only populated in an active HTTP request.

The global request object is used to access incoming request data.

Flask parses the incoming request data for us and gives us access to it through the request object.

# Accessing the method attribute on the request object

You will most often have to access the method property on the request object.

main.py
from flask import Flask, request app = Flask(__name__) @app.route("/") def hello_world(): print(request.method) if request.method == 'GET': return "<p>Request method is GET</p>" elif request.method == 'POST': return "<p>Request method is POST</p>" else: return "<p>Request method is not GET or POST</p>"

The method property returns the method of the HTTP request, e.g. GET or POST.

You can use the method property on the request to conditionally return a different response depending on the HTTP verb.

If the error persists, follow the instructions in my ModuleNotFoundError: No module named 'requests' article.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev