Skip to main content Compiler
Compiler
Picture of a console log with numpy is not defined error

Numpy (np) is not defined

Published: 2024-02-01

“numpy is not defined” is a common error experienced when working with math in python. This error occurs when you try to use a function from the numpy library without importing it first.

How to fix “numpy is not defined”

To fix this error, you need to import the numpy library first:

import numpy as np

Example

print(np.sqrt(4))

This code will result in the following error:

NameError: name 'np' is not defined

But if you import numpy first, it will work:

import numpy as np

print(np.sqrt(4))
2.0

Conclusion

To fix the “numpy is not defined” error, you need to import the numpy library first. This will allow you to use functions from the numpy library in your code.

And that’s it! You should now be able to fix this error in your code –– always make sure you have the correct libraries imported.