Last updated: Apr 8, 2024
Reading timeยท4 min
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.
Open your terminal in your project's root directory and install the requests module.
# ๐๏ธ 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
requests
module before using itAfter you install the requests module, make sure to import it before using it.
# ๐๏ธ import requests import requests def make_request(): res = requests.get('https://reqres.in/api/users', timeout=10) print(res.json()) make_request()
The example shows how to make a GET request to a remote API using the requests
module.
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.
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.
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.
requests
module in a nested scopeMake sure you haven't imported requests
in a nested scope, e.g. a function.
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')
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.
# โ 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.
requests
module in a try/except statementYou also should be importing the requests
module in a try/except
statement.
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.
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
.
# ๐๏ธ 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.
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.
request
objectYou will most often have to access the method
property on the request
object.
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.