[Solved] NameError: name ‘np’ is not defined

In Python,  NameError: name ‘np’ is not defined occurs when you import the NumPy library but fail to provide the alias as np while importing it.

In this article, let us look at what is NameError name np is not defined and how to resolve this error with examples.

Solution NameError: name ‘np’ is not defined

Let us take a simple example to reproduce this error. In the below example, we have imported the NumPy library and defined a NumPy array. 

# import numpy library
import numpy 

# define numpy array
array = np.array([[12, 33], [21, 45]]) 

# print values in array format
print(array)

Output

Traceback (most recent call last):
  File "C:\Personal\IJS\Code\main.py", line 5, in <module>
    array = np.array([[12, 33], [21, 45]])
NameError: name 'np' is not defined

When we run the code, we get  NameError: name ‘np’ is not defined  since we did not provide an alias while importing the NumPy library.

There are multiple ways to resolve this issue. Let us look at all the approaches to solve the NameError.

Method 1 – Importing NumPy with Alias as np

The simplest way to resolve this error is by providing an alias as np while importing the NumPy library. Let’s fix our code by providing an alias and see what happens.

# import numpy library
import numpy as np

# define numpy array
array = np.array([[12, 33], [21, 45]]) 

# print values in array format
print(array)

Output

[[12 33]
 [21 45]]

The syntax “import numpy as np” is commonly used because it offers a more concise way to call NumPy functions, and the code is more readable as we do not have to type “numpy” each time.

Method 2 – Importing all the functions from NumPy

There might be a situation where you need to import all the functions from the NumPy library, and to do that, we will be using the below syntax.

from numpy import *

In this case, you do not need any reference to call any functions of NumPy. You can directly call the methods without using an alias, as shown below.

# import numpy library
from numpy import *

# define numpy array
array = array([[12, 33], [21, 45]]) 

# print values in array format
print(array)

Output

[[12 33]
 [21 45]]

Method 3 – Importing NumPy package without an alias

Another way is to import a complete NumPy package and call the functions directly with the NumPy name without defining an alias.

# import numpy library
import numpy 

# define numpy array
array = numpy.array([[12, 33], [21, 45]]) 

# print values in array format
print(array)

Output

[[12 33]
 [21 45]]

In the above example, we import the complete NumPy library and use numpy.array() method to create an array.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
List of Python free ebooks

List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA…
View Post