Fix NameError Name is Not Defined

This Python error may occur because you are trying to create an array but not importing necessary packages accordingly, or the variable/identifier being accessed has not been defined in the code.

Case 1: Importation of packages

Improper importation of Python packages or failing to import the necessary packages altogether can cause this error. For example, attempting to convert the list [1, 5, 6] using the following code snippet will lead to NameError,

c = array([1, 5, 6])

Output:

NameError: name 'array' is not defined

To fix this error, we need to import the Python packages and methods correctly.

Solution 1: Using the NumPy package

The array function and all other functions in the NumPy package are accessible under the alias name np in this solution. If you are interested in the array function of NumPy only you can use the following code snippet, instead:

import numpy as np # import package
b = np.array([1.1, 2.0, 5.4]) #list to array
print(b) #print b
print(type(b))

Output:

[1.1 2.  5.4]
<class 'numpy.ndarray'>

Solution 2: Using the array package as follows

from array import array # import package
c = array("i", [1, 5, 6]) #list to array
print(c) # print the just created array
print(type(c)) # print the data-type

Output:

array('i', [1, 5, 6])
<class 'array.array'>

Note: array package requires the data type that the array will hold to be explicitly defined. The “i” string stands for integer. You can read more about array at https://docs.python.org/3/library/array.html.

Case 2: The identifier being accessed is not defined

In this case, you are trying to access a variable, or a function named “array” 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 a function/ variable before it is declared

Python executes a script from top to bottom except for functions. The contents of any function are only executed when the function is called.

print(books) # accessing a variable
books = 4 # initializing a variable

Output:

NameError: name 'books' is not defined
books_collections() # calling function
def books_collections():# declaring function 
    print(books)

Output:

NameError: name 'books_collections' is not defined

In the first case, the variable ‘books’ is referred to before it is initialized and the function ‘books_collections’ is called before it is declared in the second case, hence the error.

The NameError created here is equivalent to when we try to access a variable/function that has not been declared at all.

b) Defining a variable out of the scope

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 classroom():    
    pens = 3
    books = 4
    # pens, books declared inside function
    # they are local variables
    # rulers is declared outside function
    # it is a global variable
    print("pens (local): ", pens)
    print("rulers (global): ", rulers)

classroom()
print(pens)

Output:

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 (local variable) and therefore cannot be accessed as we are trying to do.

c) Misspelled built-in functions

For example, using “power” instead of “pow” to imply exponent leads to NameError.

power(4, 2) # it should be pow(4, 2)