shaare.it

NameError: name 'np' is not defined

7 Dec 2025

1 min read

NameError: name ‘np’ is not defined

$ python -c "arr = np.array([1,2,3])"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'np' is not defined

Why this happens

You used the common alias np but didn’t import NumPy as that alias in the current scope. This can happen when you forget import numpy as np or if the import failed.

Fix

Import NumPy with the standard alias (import numpy as np) at the top of your script or interactive session. Ensure the import succeeded (no ImportError).

Wrong code

arr = np.array([1, 2, 3])

Fixed code

import numpy as np
arr = np.array([1, 2, 3])
print(arr)