When working with the NumPy library in Python, the NameError: name 'np' is not defined is one of the most common errors for beginners. The alias np is a community-accepted convention for NumPy, not a built-in name. The error occurs when you try to use this alias without defining it in your import statement.
The fix is simple: you must ensure that your import statement correctly creates the np alias before it is used. This guide will explain the convention, show you how to fix the error, and clarify why some other import patterns can also cause this issue.
Understanding the Error: The np Alias Convention
In Python, when you import a library, you create a name in your current scope that refers to that library. The import numpy as np statement does two things:
- It imports the
numpylibrary. - It creates a shorter name, or alias,
np, that you can use to refer to it.
The NameError occurs when Python encounters the name np but has no definition for it in its memory. This happens if you only write import numpy but then try to use np.
Reproducing the NameError
The error is triggered when there is a mismatch between how you import the library and how you use it.
Example of code causing the error:
import numpy
arr = np.array([1, 2, 3])
print(arr)Output:
Traceback (most recent call last):
File "main.py", line 5, in <module>
arr = np.array([1, 2, 3])
NameError: name 'np' is not definedSolution 1: Import numpy with the as np Alias (Recommended)
The standard, community-accepted way to use NumPy is to import it with the np alias. This makes your code more concise and universally understood by other Python developers.
Solution:
import numpy as np
arr = np.array([1, 2, 3])
print(arr)Output:
[1 2 3]Solution 2: Use the Full Library Name numpy
If you choose not to use an alias, you can fix the error by using the full library name, numpy, every time you call one of its functions or classes.
Solution:
import numpy
arr = numpy.array([1, 2, 3])
print(arr)Output:
[1 2 3]While this works perfectly, it is more verbose and less common than using the pd alias.
A Note on Star Imports (from numpy import *)
Another import pattern that can cause this error is the "star import." This syntax imports all of NumPy's functions and classes directly into your current namespace.
Example of code causing the error:
from numpy import *
arr = np.array([1, 2, 3])Output:
Traceback (most recent call last):
File "main.py", line 4, in <module>
arr = np.array([1, 2, 3])
NameError: name 'np' is not definedWhen you use a star import, you must call the functions by their names directly, without any prefix.
The Correct Usage (but still bad practice):
from numpy import *
arr = array([1, 2, 3])
print(arr)Output:
[1 2 3]Conclusion
| If your code has... | The fix is... |
|---|---|
import numpy and np.array() |
Change the import to import numpy as np. |
from numpy import * and np.array() |
Change the import to import numpy as np. |
import numpy and you want to keep it |
Change the usage to numpy.array(). |
To avoid the NameError: name 'np' is not defined and write code that is easy for others to understand, you should always follow the convention of importing the library with import numpy as np at the top of your script.