Nameerror name np is not defined

When working with Python, you might encounter the error message nameerror name ‘np’ is not defined.

Today, we will walk you through what this error means and how to fix it.

Continue reading because we’ll hand you the solutions to this nameerror: name ‘np’ is not defined.

What is “nameerror name np is not defined”?

The error message nameerror: name ‘np’ is not defined occurs when you try to import NumPy module in your Python script.

However, you forget to define the alias “np” when you are importing the module.

Here’s the following code that will result in this error.

import numpy
sample = np.random.normal(1, 2, 3)
print(sample)

As we run this code, it will throw an error message.

Traceback (most recent call last):
  File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 2, in <module>
    sample = np.random.normal(1, 2, 3)
             ^^
NameError: name 'np' is not defined

How to fix “nameerror name np is not defined”?

You can fix the nameerror: name ‘np’ is not defined error by providing the alias of np when importing NumPy.

Alternatively, you can replace all np syntax with numpy in your Python script.

1. Import the numpy module correctly

You have to provide the alias of np when you are importing NumPy.

✅ import numpy as np

Here’s the solution for the example error above:

 import numpy as np

sample = np.random.normal(1, 2, 3)
print(sample)

Take note the “import numpy as np” is frequently used by many because it is the easy and concise way to use the functions of NumPy.

The reason is that you will not type “numpy” every time, but instead, you can simply type “np.”

Output:

[-2.90642325  0.27431138 -0.3864386 ]

2. Use numpy instead of np

You can also use numpy instead of np in your script.

For example:

✅ import numpy

sample = numpy.random.normal(1, 2, 3)
print(sample)


This code imports the NumPy module and uses numpy instead of np in the code.

Output:

[-1.23237585  2.7055014   2.07939488]

3. Import specific functions from NumPy

You can also import specific functions from NumPy that you need in your Python script.

For example:

✅ from numpy import random
sample = random.normal(1, 2, 3)
print(sample)

Output:

[ 5.10634827 -4.69797554 -1.24238393]

4. Use from numpy import *

✅ from numpy import *
sample = random.randint(low=10, high=30, size=6)
print(sample)

Output:

[27 29 29 24 18 13]

Conclusion

In conclusion, the nameerror name ‘np’ is not defined error message occurs when using the NumPy module in your Python script. 

Especially when you try to import the NumPy module but forget to define the alias “np.”

The solution for this error is whether you want to use np or numpy in your Python script.

If you want to use np, add np to the import numpy statement, or you can replace all np with numpy in your script.

This article already provides solutions for this error to help you fix the Python error message.

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊