Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'array' is not defined in python

Tags:

python

arrays

I get NameError: name 'array' is not defined in python error when I want to create array, for example:

a = array([1,8,3])

What am I doing wrong? How to use arrays?

like image 670
Templar Avatar asked Aug 17 '11 20:08

Templar


People also ask

How do I fix NameError is not defined in Python?

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.

How do you define an array in Python?

What are Python Arrays? Arrays are a fundamental data structure, and an important part of most programming languages. In Python, they are containers which are able to store more than one item at the same time. Specifically, they are an ordered collection of elements with every value being of the same data type.

Why array is not defined?

The Python "NameError: name 'array' is not defined" occurs when we use the array module without importing it first. To solve the error, import from the array module before using it - from array import array . If using numpy , access array on the numpy module, e.g. np. array .

What is the NameError in Python?

What Is a NameError in Python? In Python, the NameError occurs when you try to use a variable, function, or module that doesn't exist or wasn't used in a valid way. Some of the common mistakes that cause this error are: Using a variable or function name that is yet to be defined.


1 Answers

You need to import the array method from the module.

from array import array

http://docs.python.org/library/array.html

like image 80
逆さま Avatar answered Sep 20 '22 14:09

逆さま