Python 在 Windows 上安装 NumPy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28413824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing NumPy on Windows
提问by Mridu Bhattacharya
I'm simply unable to install NumPy on Windows. I keep getting this error -
我只是无法在 Windows 上安装 NumPy。我不断收到此错误 -
PS C:\python27> pip install http://sourceforge.net/projects/numpy/file/NumPy/
Collecting http://sourceforge.net/projects/numpy/files/NumPy/
Downloading http://sourceforge.net/projects/numpy/files/NumPy/ (58kB)
100% |################################| 61kB 15kB/s
Cannot unpack file c:\users\toshiba\appdata\local\temp\pip-qev4rz-unpack\NumPy
(downloaded from c:\users\toshiba\appdata\local\temp\pip-omripn-build, content-type: text/html; charset=utf-8); cannot detect archive format
Cannot determine archive format of c:\users\toshiba\appdata\local\temp\pip-omripn-build
I had Python 64 bit version earlier and I was not sure if NumPy version was compatible with 64 bit Python. So I uninstalled it and installed 32 bit Python version. But still I'm getting the same error. Though my Python 32 bit version is working fine.
我之前有 Python 64 位版本,我不确定 NumPy 版本是否与 64 位 Python 兼容。所以我卸载了它并安装了 32 位 Python 版本。但我仍然遇到同样的错误。虽然我的 Python 32 位版本运行良好。
I tried "pip install numpy" but that give me the following error at the end -
我试过“pip install numpy”,但最后给了我以下错误 -
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'define_macros'
warnings.warn(msg)
error: Unable to find vcvarsall.bat
----------------------------------------
Command "C:\Python27\python.exe -c "import setuptools,tokenize;__file__='c:\users\toshiba\appdata\local\temp\pip-build-hdhqex\numpy\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'),__file__, 'exec'))" install --record c:\users\toshiba\appdata\local\temp\pip-x_6llm-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in c:\users\toshiba\appdata\local\temp\pip-build-hdhqex\numpy
Please tell me what I might be doing wrong.
请告诉我我可能做错了什么。
采纳答案by Yannick Meine
Some explanations
一些解释
In the first case, I didn't check but I guess that pip
directly downloads the resource corresponding to the given URL: http://sourceforge.net/projects/numpy/file/NumPy/. The server returns a HTML document, while pip
expects an archive one. So that can't work.
在第一种情况下,我没有检查,但我想pip
直接下载与给定 URL 对应的资源:http://sourceforge.net/projects/numpy/file/NumPy/。服务器返回一个 HTML 文档,而pip
期望一个存档文档。所以这是行不通的。
Then there are basically two ways to install Python packages:
那么安装Python包基本上有两种方式:
- from sources, as you tried then
- from pre-compiled packages
- 来自消息来源,正如您当时所尝试的那样
- 从预编译包
The first case, you tried it with the command pip install numpy
, but since this package contains native code, it requires development tools to be installed properly (which I always found to be a pain in the neck to do on Windows, but I did it so it's clearly feasible). The error you have error: Unable to find vcvarsall.bat
means you don't have the tools installed, or the environment properly set up.
第一种情况,您使用命令进行了尝试pip install numpy
,但是由于该软件包包含本机代码,因此需要正确安装开发工具(我一直发现在 Windows 上这样做很麻烦,但我这样做了显然可行)。您遇到的错误error: Unable to find vcvarsall.bat
意味着您没有安装工具,或者没有正确设置环境。
For the second case, you have different kinds of pre-compiled packages:
对于第二种情况,您有不同种类的预编译包:
- wheels, which you install with
pip
as well - installers, which you use as standard installers on Windows
- 车轮,其安装使用
pip
以及 - 安装程序,您在 Windows 上用作标准安装程序
For both, you need to check that the binary has been strictly compiled for your Python architecture (32 or 64 bits) and version.
对于两者,您都需要检查二进制文件是否已针对您的 Python 架构(32 位或 64 位)和版本进行了严格编译。
An easy solution
一个简单的解决方案
You can find there several wheels for numpy
: http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy. To get the proper architecture, check in the name win32
for 32 bits and amd64
for 64 bits. To get the proper Python version, check cpXX
: first X is major version, and second X is minor version, so for instance cp27
means CPython 2.7.
你可以在那里找到几个轮子numpy
:http: //www.lfd.uci.edu/~gohlke/pythonlibs/#numpy。要获得正确的架构,请检查win32
32 位和amd64
64 位的名称。要获得正确的 Python 版本,请检查cpXX
:第一个 X 是主要版本,第二个 X 是次要版本,例如cp27
表示 CPython 2.7。
Example: pip install numpy?1.9.2rc1+mkl?cp27?none?win32.whl
例子: pip install numpy?1.9.2rc1+mkl?cp27?none?win32.whl
The hard solution: installing and using development tools
硬解:安装和使用开发工具
DISCLAIMER: all the following explanations might not be quite clear. They result from several investigations at different moments, but in my configuration they led to a working solution. Some links might be useless, or redundant, but that's what I noted. All of this requires a bit of cleaning, and probably generalization too.
免责声明:以下所有解释可能不太清楚。它们来自不同时刻的多次调查,但在我的配置中,它们导致了一个可行的解决方案。有些链接可能无用或多余,但这就是我所注意到的。所有这些都需要一些清理,可能也需要泛化。
First, you need to understand that disutils
- which is the pre-installed package which handles packages workflow at lower level than pip
(and which is used by the latter) - will try to use a compiler that strictly matches the one that was used to build the Python machine you installed.
首先,您需要了解disutils
- 这是在较低级别处理包工作流的预安装包pip
(并由后者使用) - 将尝试使用与用于构建的编译器严格匹配的编译器你安装的 Python 机器。
Official distributions of Python use Microsoft Visual C++ for Microsoft Windows packages. So you will need to install this compiler in this case.
Python 的官方发行版使用 Microsoft Visual C++ for Microsoft Windows 包。所以在这种情况下你需要安装这个编译器。
How to find proper version of Visual C++
如何找到正确版本的 Visual C++
The string printed by Python with this command python -c "import sys; print(sys.version)"
(or when you invoke the interactive shell) will look like this:
Python 使用此命令python -c "import sys; print(sys.version)"
(或调用交互式 shell 时)打印的字符串将如下所示:
3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)]
3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)]
The last part between square brackets is the identification part of the compiler. Unfortunately, this is not quite straightforward, and you have correspondence lists there:
方括号之间的最后一部分是编译器的标识部分。不幸的是,这不是很简单,您在那里有通信列表:
In the example I gave above, this means Microsoft Visual C++ 2010 64 bits.
在我上面给出的示例中,这意味着Microsoft Visual C++ 2010 64 bits。
How to install Visual C++
如何安装 Visual C++
You cannot find anymore a standalone package of Visual C++ for modern versions. So you will need to install the Windows SDK itself.
您再也找不到适用于现代版本的独立 Visual C++ 包了。因此,您需要安装 Windows SDK 本身。
Here are some reference links:
以下是一些参考链接:
- Download Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1 from Official Microsoft Download Center: for Visual C++ 15.00 (Visual Studio 2008). Corresponds to WinSDK 7.
- Download Microsoft Windows SDK for Windows 7 and .NET Framework 4 from Official Microsoft Download Center: for Visual C++ 16.00 (Visual Studio 2010). Corresponds to WinSDK 7.1.
- installation - where can I download the full installer for Visual C++ Express? - Super User
- 从 Microsoft 官方下载中心下载适用于 Windows 7 和 .NET Framework 3.5 SP1 的 Microsoft Windows SDK:适用于 Visual C++ 15.00 (Visual Studio 2008)。对应于 WinSDK 7。
- 从 Microsoft 官方下载中心下载适用于 Windows 7 和 .NET Framework 4 的 Microsoft Windows SDK:适用于 Visual C++ 16.00 (Visual Studio 2010)。对应于 WinSDK 7.1。
- 安装 - 在哪里可以下载 Visual C++ Express 的完整安装程序?- 超级用户
Troubleshooting
故障排除
You might have an error at the installation of the SDK:
DDSet_Error: Patch Hooks: Missing required property 'ProductFamily': Setup cannot continue.
DDSet_Warning: Setup failed while calling 'getDLLName'. System error: Cannot create a file when that file already exists.
您可能在安装 SDK 时遇到错误:
DDSet_Error: Patch Hooks: Missing required property 'ProductFamily': Setup cannot continue.
DDSet_Warning: Setup failed while calling 'getDLLName'. System error: Cannot create a file when that file already exists.
They have been reported in several questions already:
他们已经在几个问题中得到了报道:
- Windows 7 SDK Installation Failure
- Error installing Windows 7 SDK 7.1 with VS2008, VS2010 Premium on Win 7 32bit
As a solution, you can check this link: Windows SDK Fails to Install with Return Code 5100
作为解决方案,您可以查看此链接:Windows SDK Fails to Install with Return Code 5100
The thing is to remove all conflicting (understand: the ones that the SDK installer tries to install itself) version of the Visual C++ redistributable.
问题是删除 Visual C++ 可再发行组件的所有冲突(理解:SDK 安装程序尝试自行安装的版本)版本。
Use development tools
使用开发工具
Normally you should run vsvarsall.bat
(located inside the VC
folder of the installation path of Visual Studio - example: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat
) to set up the proper environment variables so that the execution of distutils
doesn't fail when trying to compile a package.
通常,您应该运行vsvarsall.bat
(位于VC
Visual Studio 安装路径的文件夹内- 示例:)C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat
来设置正确的环境变量,以便distutils
在尝试编译包时不会失败。
This batch script accepts a parameter, which should set the wanted architecture. However I saw that with the free versions of the SDK some additional scripts were missing when trying several of these parameters.
此批处理脚本接受一个参数,该参数应设置所需的架构。但是,我发现使用 SDK 的免费版本时,在尝试其中几个参数时缺少一些额外的脚本。
Just to say that if you are compiling for a 32 bits architecture, simply calling vsvarsall.bat
should work. If you need to compile for 64 bits, you can directly call SetEnv.cmd
, located somewhere under inside the SDK installation path - example: "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64
.
只是说,如果您正在为 32 位架构进行编译,则只需调用即可vsvarsall.bat
。如果需要编译64位,可以直接调用SetEnv.cmd
,位于SDK安装路径下的某处——例如:"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64
.
回答by Dr. Jan-Philip Gehrcke
On Windows, pip
is great for installing packages that do not require compiling. Otherwise, seriously, save yourself the hassle of building and maintaining packages, and take advantage of the work others did for you. I recommend using either of these Python distributions:
在 Windows 上,pip
非常适合安装不需要编译的包。否则,认真地,省去构建和维护包的麻烦,并利用其他人为您所做的工作。我建议使用以下任一 Python 发行版:
Anaconda is a little larger to download and install, but it includes many useful third-party packages by default (such as numpy). ActivePython includes a package manager which allows you to easily install pre-compiled binaries (installing numpy is as easy as pypm install numpy
).
Anaconda 的下载和安装稍大一些,但它默认包含许多有用的第三方软件包(例如 numpy)。ActivePython 包括一个包管理器,它允许您轻松安装预编译的二进制文件(安装 numpy 就像 一样简单pypm install numpy
)。
The advantage of using these Python distributions is that you can get a working installation running in minutes, in an easily reproducible manner.
使用这些 Python 发行版的优势在于,您可以在几分钟内以一种易于重现的方式运行工作安装。
回答by Sushma R
I tried to install numpy for windows 7, 64-bit and spent quite sometime. I was actually trying to setup sklearn. Researched many posts, documented what worked for me. Hope it saves your time! https://simplemachinelearning.wordpress.com/2015/11/09/set-up-sklearn-on-windows/
我尝试为 64 位 Windows 7 安装 numpy 并花了很长时间。我实际上是在尝试设置 sklearn。研究了很多帖子,记录了对我有用的东西。希望它能节省您的时间! https://simplemachinelearning.wordpress.com/2015/11/09/set-up-sklearn-on-windows/
回答by be_good_do_good
Best solution for this is to download and install VCforPython2.7 from https://www.microsoft.com/en-us/download/details.aspx?id=44266
最佳解决方案是从https://www.microsoft.com/en-us/download/details.aspx?id=44266下载并安装 VCforPython2.7
Then try pip install numpy.
然后尝试 pip install numpy。
100% working
100% 工作
回答by Vikram S
I too faced the above problem while setting up python for machine learning.
我在为机器学习设置 python 时也遇到了上述问题。
I followed the below steps :-
我遵循以下步骤:-
Install python-2.7.13.msi
安装 python-2.7.13.msi
? set PATH=C:\Python27
? 设置 PATH=C:\Python27
? set PATH=C:\Python27\Scripts
? 设置 PATH=C:\Python27\Scripts
Go to http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy
转到http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy
Downloaded:- ? numpy-1.13.1+mkl-cp27-cp27m-win32.whl ? scipy-0.18.0-cp27-cp27m-win32.whl
下载:- ?numpy-1.13.1+mkl-cp27-cp27m-win32.whl ?scipy-0.18.0-cp27-cp27m-win32.whl
Installing numpy: pip install numpy-1.13.1+mkl-cp27-cp27m-win32.whl
安装numpy:pip install numpy-1.13.1+mkl-cp27-cp27m-win32.whl
Installing scipy: pip install scipy-0.18.0-cp27-cp27m-win32.whl
安装scipy:pip install scipy-0.18.0-cp27-cp27m-win32.whl
You can test the correctness using below cmds:-
您可以使用以下 cmds 测试正确性:-
>>> import numpy
>>> import scipy
>>> import sklearn
>>> numpy.version.version
'1.13.1'
>>> scipy.version.version
'0.19.1'
>>>