>>> os.getcwd()
'C:\\Users\\clu\\AppData\\Local\\Programs\\Python\\Python310'
Error NameError: name 'np' is not defined [closed]
回答1
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
回答2
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).
Error "Import Error: No module named numpy" on Windows
回答1
pip3 install numpy
上面这个可能不行,要用pip2
pip install numpy
or pip3 install numpy
did not work as they defaulted the installation to python 3's package folders (for unknown reasons). I used pip2 install numpy
to resolve the errors for "no module found...".
pip3 install numpy
the numpy package was installed into a specific version, and when you tried import numpy
you used another python version. This happens to me all the time. Make sure that the environment / python version where you install/run the package is the same.
May 25, 2020 at 8:49{
"date": "2022-09-09T08:07:53-0500",
"dirty": false,
"error": null,
"full-revisionid": "e47cbb69bebf36007c3ea009aee03e4bfe3b3f3d",
"version": "1.23.3"
}
''' # END VERSION_JSON
Found existing installation: numpy 1.23.3
Uninstalling numpy-1.23.3:
Would remove:
c:\users\clu\appdata\local\programs\python\python310\lib\site-packages\numpy-1.23.3.dist-info\*
c:\users\clu\appdata\local\programs\python\python310\lib\site-packages\numpy\*
c:\users\clu\appdata\local\programs\python\python310\scripts\f2py.exe
Support for Python 3 was added in NumPy version 1.5.0, so to begin with, you must download/install a newer version of NumPy.
Or simply using pip
:
python3 -m pip install numpy
pip3 install numpy
by default it installs the numpy version 1.18.4, use specific version instead like this - pip install numpy==1.8.2
. check official doc for details- pypi.org/project/numpy/1.8.2
May 11, 2020 at 20:59Installing numpy with pip on windows 10 for python 3.7
回答1
Installing NumPy on Windows is a common problem if you don't have the right build setup. Instead, I always go to Christoph Gohlke's website to download the wheels you can install for your computer. Christoph generously builds the libraries himself with the right build environment and he posts it on his website.
Newer Instructions - For older instructions, please scroll down
First, install pipwin
from PyPI which will install a utility that acts like pip
but it will download the actual package you're interested in from his website, then use pipwin install
to install the package you want.
First do:
pip install pipwin
When that's installed, you can then do:
pipwin install numpy
This will install the latest version of NumPy on your system. This way you don't have to specifically search for the version of NumPy that is for your specific version of Python.
Older instructions
Go to the NumPy section: https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy then download the version for 3.7 that is compatible with your version of Python (2 or 3 and 32-bit or 64-bit). For example, the filename numpy‑1.14.5+mkl‑cp37‑cp37m‑win_amd64.whl
is for NumPy 1.14.5, Python 3.7 - 64 bit. You can pick out which version of NumPy and which version of the Python interpreter and bit version you need in the filename.
Doing this never requires you to build NumPy yourself or install the required compiler as opposed to installing NumPy through PyPI. You can just download the wheel and install it yourself. Assuming you've already downloaded it, just do:
pip install numpy‑1.14.5+mkl‑cp37‑cp37m‑win_amd64.whl
... assuming the wheel is in the directory you're currently in.
成功安装
C:\WINDOWS\system32>pipwin install numpy
Building cache. Hang on . . .
Done
Package `numpy` found in cache
Downloading package . . .
https://download.lfd.uci.edu/pythonlibs/archived/numpy-1.22.4+vanilla-cp310-cp310-win_amd64.whl
numpy-1.22.4+vanilla-cp310-cp310-win_amd64.whl
[*] 5.8 MB / 5.8 MB @ 1.8 MB/s [##################] [100%, 0s left]
Processing c:\users\clu\pipwin\numpy-1.22.4+vanilla-cp310-cp310-win_amd64.whl
Installing collected packages: numpy
Successfully installed numpy-1.22.4+vanilla
回答2
Starting 24 November 2021, latest numpy require at least Python 3.8
Note: This might not be the original question asked, but it might help anyone come here.
To use python 3.7, latest numpy you can use is v1.21.4. So, to install it, use:
pip install numpy==1.21.4
If you write requirements that you hope compatible with python 3.7, you can use numpy<=1.21.4
EDIT: in 20 December 2021, numpy release version 1.21.5 that support Python 3.7
From comment section, by @sam, numpy 1.21.5 support Python 3.7. It was released after 1.22.0rc1 (the latest numpy version as the writing of the original post) that only support Python 3.8++.
Lesson learned from this experience, it would be better to use <
,
pip install numpy<1.22.0
or
install_requires = [
"numpy<1.22.0", # lates version to support python 3.7
],