NameError can occur for two reasons: we are trying to use the “requests” package before importing it, or the word “requests” is used as an identifier before being refined.
The package “requests” in Python is used to send HTTP requests to a website. It returns a response object based on the request type sent.
It can contain website content and the response’s status code, among other things. The “requests” package has the following methods (request types): get, post, head, delete, patch, put and request (read more in requests documentation).
url = "https://en.wikipedia.org/wiki/Gross_domestic_product" r = requests.get(url) print(r.text)
NameError: name 'requests' is not defined
We attempt to use “requests” before importing it in the above code snippet.
The solution for this error, in this case, is to import the “requests” package before using it. Before doing this, make sure that “requests” is installed by running “pip install requests” on the terminal or running “conda install requests” for conda users. If the package is not installed, you will run into another error “ModuleNotFoundError: No module named ‘requests'”
The error on the code snippet above can be solved as follows:
import requests url = "https://en.wikipedia.org/wiki/Gross_domestic_product" r = requests.get(url)
Here is another example of an appropriate way of using the “requests” package.
import requests from bs4 import BeautifulSoup url = "https://en.wikipedia.org/wiki/Lists_of_countries_by_GDP" page = requests.get(url) soup = BeautifulSoup(page.text, features="lxml") links = soup.find_all('a') for link in links: print(link.text) print(link.get("href"))
Screenshot of a section of the website (https://en.wikipedia.org/wiki/Lists_of_countries_by_GDP):
The output of the printing page.text (truncated):
The output of the for-loop (truncated):
>>> List of countries by past and projected GDP (nominal) /wiki/List_of_countries_by_past_and_projected_GDP_(nominal) List of countries by past and projected GDP (PPP) /wiki/List_of_countries_by_past_and_projected_GDP_(PPP) List of countries by past and projected GDP (nominal) per capita /wiki/List_of_countries_by_past_and_projected_GDP_(nominal)_per_capita <<<
In this code example, we imported the “requests” package first and then used it to extract the contents of a Wikipedia page.
We also make use of BeautifulSoup and the lxml parser to extract information from the pulled website data (you may have to install “bs4” and “lxml” before running the code) (BeautifulSoup is a useful web scraping tool – read more on the documentation).
soup.find_all(‘a’) finds all the links on the website, that is, all contents of <a> tag. We extract the text and the URL links from the contents on a for-loop.
Case 2: The identifier being accessed is not defined
Code execution can also lead to NameError if we try to access a variable or function named “requests” or otherwise when it has not been defined or has been defined in a different scope. This case captures all other causes of NameError in Python. They include:
a) Calling function/ variable before defining it
Python executes a script from top to bottom except for functions. The contents of any function are only executed when the function is called. Any attempt to call an identifier before defining it will lead to NameError. For example:
request = 39 print(requests)
NameError: name 'requests' is not defined
def values_count(): items = 56 + 3 return items value_count()
NameError: name 'value_count' is not defined
In the above cases, the identifiers are misspelled. In the first case, the print() function is trying to access “requests” before it is defined, and in the second case, the function “value_count()” is called before being declared. The function defined is “values_count()”.
The variable defined inside the function is called a local variable and can only be accessed inside the function. In contrast, a variable defined outside a function (global variable) is accessible anywhere in the script after being declared. Referencing a local variable outside the function causes NameError. Here is an example.
rulers = 8 def items_count(): pens = 3 books = 4 # pens, books declared inside the function # they are local variables # rulers is declared outside the function # it is a global variable print("pens (local): ", pens) print("rulers (global): ", rulers) items_count() print(pens)
pens (local): 3 rulers (global): 8 NameError: name 'pens' is not defined
The last line leads to NameError because the “pens” variable is defined inside the function (a local variable) and therefore cannot be accessed outside as we are trying to do.