This error typically occurs when you try to use the NumPy library but do not define the alias np when importing the module. You can solve this error by using the as
keyword to alias the numpy
module, for example:
import numpy as np
This tutorial will go through how to solve this error with code examples.
Table of contents
NameError name ‘np’ is not defined
Python raises the NameError when it cannot recognise a name in our program. In other words, the name we are trying to use is not defined in the local or global scope. A name can be related to a built-in function, module, or something we define in our programs, like a variable or a function.
The error typically arises when:
- We misspell a name
- We do not define a variable or function
- We do not import a module
In this tutorial, the source of the error NameError: name ‘np’ is not defined is due to either not aliasing or incorrectly aliasing the numpy module. Let’s look at an example.
Example
Let’s look at an example of creating a NumPy ndarray using the numpy
module. First, we must have numpy
installed. You can go to the following article to learn how to install numpy
for your operating system: How to Solve Python ModuleNotFoundError: no module named ‘numpy’.
Once we have numpy
installed, we can try to create a ndarray
using the array()
method as follows:
import numpy arr = np.array([2, 4, 6, 8]) print(arr)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [1], in <cell line: 3>() 1 import numpy ----> 3 arr = np.array([2, 4, 6, 8]) 5 print(arr) NameError: name 'np' is not defined
The error occurs because we installed numpy
but did not correctly alias the module as np
. Therefore, the name np
is not defined and we cannot create an ndarray
.
Solution #1: Use the as keyword
The easiest way to solve this error is to use the as
keyword to create the alias np
. Let’s look at the updated code:
import numpy as np arr = np.array([2, 4, 6, 8]) print(arr)
Let’s run the code to get the ndarray.
[2 4 6 8]
Solution #2: Do not use aliasing
We can also solve this error by removing the alias and using the full name of the module. Let’s look at the revised code:
import numpy arr = numpy.array([2, 4, 6, 8]) print(arr)
Let’s run the code to get the array:
[2 4 6 8]
Solution #3: Use the from keyword
We can also use the from
keyword to import a specific variable, class, or function from a module. In this case, we want to import the array function from the numpy module. Using the from
keyword means we do not have to specify the module in the rest of the program, we only need to call the array function. Let’s look at the revised code:
from numpy import array arr = array([2, 4, 6, 8]) print(arr)
[2 4 6 8]
Using the from
keyword can help make programs more concise and readable. If you want to import more than one class or function from the numpy
module you can use commas between the imports. For example:
from numpy import array, square arr = array([2, 4, 6, 8]) square_vals = square(arr) print(square_vals)
In the above code we imported the array
and square
functions to create an array of integers and then create an array with the squares of the integers. Let’s run the code to see the result:
[ 4 16 36 64]
The standard use of numpy
is to import and alias the module and access the classes or methods when needed in the program using np.
.
Summary
Congratulations on reading to the end of this tutorial.
For further reading on errors involving NameErrors, go to the articles:
- How to Solve Python NameError: name ‘xrange’ is not defined
- How to Solve Python NameError: name ‘os’ is not defined
- How to Solve Python NameError: name ‘pd’ is not defined
- How to Solve Python NameError: name ‘plt’ is not defined
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.