from numpy import * x = np.random.randint(low=10, high=30, size=6) print(x) "C:\Users\Piistasyo\PycharmProjects\test project\venv\Scripts\python.exe" "C:/Users/Piistasyo/PycharmProjects/test project/loop.py" Traceback (most recent call last): File "C:/Users/Piistasyo/PycharmProjects/test project/loop.py", line 44, in <module> x = np.random.randint(low=10, high=30, size=6) NameError: name 'np' is not defined why am i getting this error? pls help i already installed the numpy package
42 Answers
As @aydow says, "change from numpy import * to import numpy as np":
import numpy as np ... Or don't write np:
from numpy import * x = random.randint(low=10, high=30, size=6) ... Because, from numpy import *, Import every function in numpy, so np is not a function of numpy, so have to Import numpy like import numpy as np, Or, Remove np part of np.random.randint(low=10, high=30, size=6), and make it like this: random.randint(low=10, high=30, size=6), it's all since random is a function of numpy, basically that's all, to explain
You haven't defined np.
The first thing you're currently doing is
from numpy import * This imports the package numpy, and everything inside of that package. However, numpy does not contain a module called np. The typical practice for numpy is to instead do
import numpy as np This imports just the package numpy, and renames it to np so that you can dereference it by using the dot operator on np. This allows you to call np.random(), since random is a member of numpy, which is aliased to np.
With what you're currently doing, you could do either numpy.random() or just random (since it was part of the * that you imported from numpy).