Nameerror name requests is not defined

In this article, we will show you how to fix the Python nameerror name requests is not defined error message.

If you’re new to this error message technically, you don’t any idea how to resolve it.

Luckily, this article got you back because we’ll discuss what is nameerror: name requests is not defined error means, why it occurs, and how to fix this error.

So, just keep reading!

What is “nameerror: name requests is not defined”?

The Python nameerror name requests is not defined error message occurs when you’re trying to use the “request” module but did not import the module first.

Or the request module is not installed in your environment.

For example:

response = requests.get('https://www.google.com')
print(response.status_code)

If you try to run this code, as a results will throw an error message.

NameError: name 'requests' is not defined

The error occurs because you’re trying to use the requests module without importing it first.

Why does the “nameerror name requests is not defined”?

This error can be caused by several factors:

❌ Typically because the requests module has not been imported at the beginning of the Python script.

❌ If you forget to import the Requests library.

❌ If there’s a typo in your import statement.

How to fix “nameerror: name requests is not defined”?

To fix the nameerror: name ‘requests’ is not defined, you need to install the module and then import the requests module before using it.

Here are some solutions to fix the error:

1. Install the “requests” module

One of the reasons why you are facing this error is that you did not install the request module.

You can install the requests module using pip by running one of the following commands in your terminal.

For Python 2:

pip install requests

For Python 3:

✅ pip3 install requests
or
✅ python3 -m pip install requests

For Anaconda:

✅ conda install -c anaconda requests

For Ubuntu/Debian:

✅ sudo apt-get install python3-requests

For CentOS:

✅  sudo yum install python-requests

If you get permissions error:

✅ sudo pip3 install requests

If you don’t have pip in your PATH environment variable:

✅ python -m pip install requests

2. Import “requests” module

After you install the requests module successfully, the next step is to ensure that the module is imported into your code.

For example:

import requests

response = requests.get('https://www.google.com')
print(response.text)

Take note: you have to import the module at the top or at the beginning of you code to be able to use it throughout your code.

This is the whole process:

1.  Install the requests module
pip install requests

2.  Import the requests module
import requests

3. Use the requests module
response = requests.get('https://www.google.com')
print(response.text)

Conclusion

The Python nameerror name requests is not defined error message occurs when you’re trying to use the “request” module but did not import the module first.

Or the request module is not installed in your environment.

This article discusses what this error is all about and already provides solutions to help you fix this error.

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Frequently Asked Questions

What is Python NameError and what causes it?

NameError is raised when Python encounters a name (variable, function, class) that hasn’t been defined in the current scope. Most common causes: typo in variable name, using a variable before assigning it, missing import, or referencing a variable that was defined inside a function but accessed outside it.

How do I fix ‘name X is not defined’?

Check three things: (1) Is the name a typo? Compare with the spelling where you defined it. (2) Did you import it? Add ‘from module import X’ or ‘import module’ at the top. (3) Is X defined in a different scope (inside a function, conditional branch, or with-block) that hasn’t executed yet at the point you’re using it? Move the definition before the use.

Why does my variable work in one cell but not another (Jupyter)?

Jupyter kernels keep state between cells. If you defined X in cell 5 and run cell 3 later, X exists. But after Kernel-Restart, only the cells you re-run define their variables. Always run cells top-to-bottom on a fresh kernel before submitting. Use ‘Restart and Run All’ to verify your notebook is reproducible.

What is the difference between NameError and AttributeError?

NameError: the name itself doesn’t exist anywhere in scope (typo, missing import, scope issue). AttributeError: the name exists and points to an object, but that object has no such attribute/method (typo on method name, wrong object type). NameError is about the variable; AttributeError is about what’s inside it.

Where can I find more NameError fixes?

Browse the NameError reference hub for 49+ specific fixes (NumPy, pandas, Jupyter, Python 2 to 3 migration). For Python scope rules see the Python Tutorial hub. For attribute-level errors see AttributeError.

Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →