[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.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Exponent | Calculate Exponent In Python

Python Exponent | Calculate exponent in Python

Table of Contents Hide How To Calculate Exponent In Python?Using ** operator to calculate exponent in PythonExample Exponentiation in Python using ** operatorExample ZeroDivisionError Using pow() function to calculate exponent in…
View Post
Python String Isdigit()

Python String isdigit()

Table of Contents Hide What are the valid digits in Python?isdigit() Syntaxisdigit() Parameters isdigit() Return valueExample 1: Working with Python string isdigit() methodExample 2: How do I check if a string…
View Post
Python String Count()

Python String count()

Table of Contents Hide count() Syntaxcount() Parameter count() Return ValueExample 1: Count the number of occurrences of a given substring without optional argumentsExample 2: Count the number of occurrences of a…
View Post