Skip to main content

Python NumPy: How to Resolve "NameError: name 'np' is not defined"

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:

  1. It imports the numpy library.
  2. 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:

# The numpy library is imported, but no alias is created.
import numpy

# Python does not know what 'np' is.
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 defined

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:

# ✅ Correct: Import the library and create the 'np' alias.
import numpy as np

# Now Python knows that 'np' refers to the numpy library.
arr = np.array([1, 2, 3])
print(arr)

Output:

[1 2 3]
note

Using import numpy as np is a strong convention in the data science and scientific computing communities. Adhering to it makes your code easier for others to read and understand.

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 the library without an alias.
import numpy

# ✅ Correct: Use the full name 'numpy' throughout your code.
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 *

# 'np' is not defined with a star 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 defined

When 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 *

# This works because 'array' is now in the global namespace.
arr = array([1, 2, 3])
print(arr)

Output:

[1 2 3]
warning

Avoid Using Star Imports (from ... import *)

This practice is strongly discouraged in most production code. It pollutes your namespace by importing dozens of names, making it unclear where functions are coming from. This can lead to subtle bugs if names conflict and makes code much harder to read and debug.

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