To fix the NameError: name ‘array’ is not defined in Python, you can use the following: import the array package, import the array class from the array module, or you can use list to store the array instead. The following article will describe in detail.
What causes the NameError: name ‘array’ is not defined in Python?
You easily get the ‘NameError’ when using a variable, function, or module that is not declared or you are not using it properly.
NameError: name ‘array’ is not defined happens because you don’t import the array module before using it.
Example:
myArray = array([1,2,3,4,5]) print(myArray)
Output:
Traceback (most recent call last):
File "./prog.py", line 1, in <module>
myArray = array([1,2,3,4,5])
NameError: name 'array' is not defined
How to solve the NameError: name ‘array’ is not defined in Python?
Import the numpy package
You can import the numpy package to use the np.array function to create a numpy array.
Example:
- Import numpy package with alias np.
- Use the np.array function to create a numpy array.
# import numpy package import numpy as np # Use the np.array function to create a numpy array myArray = np.array([1,2,3,4,5]) print('Perform array output:', myArray)
Output:
Perform array output: [1 2 3 4 5]
Import the array method from the array module
Example:
- Import the array method from the array module to create an array.
- Note: The array module requires the data type that the array stores to be specified. The string ‘i’ in Python Type is ‘int’, and the minimum size in bytes is 2.
# Use the array method from the array module from array import array # Use the array method to create an array myArray = array('i',[1,2,3,4,5]) print('Perform array output:', myArray)
Output:
Perform array output: array('i', [1, 2, 3, 4, 5])
Use a list to store multiple elements
You can use the list data type to store multiple elements. It might be more convenient than arrays without importing the included libraries.
Example:
# Create a list that stores multiple elements myList = [1,2,3,4,5] print('Output the list:', myList)
Output:
Output the list: [1, 2, 3, 4, 5]
You can use the dir() function to check an object’s list of valid properties.
Example:
# The dir function returns a list of valid attributes of the object print('Valid attributes of list:', dir(list))
Output:

Summary
Above are the causes and ways you can fix the error NameError: name ‘array’ is not defined in Python. The important thing to remember is to import the numpy package or the array class from the array module and the problem will be solved. Good luck!
Maybe you are interested:
- NameError: name ‘json’ is not defined in Python
- NameError: name ‘random’ is not defined in Python
- NameError: name ‘urllib’ is not defined in Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java