Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

python - Installing NumPy on Windows

I'm simply unable to install NumPy on Windows. I keep getting this error -

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:usersoshibaappdatalocalemppip-qev4rz-unpackNumPy
(downloaded from c:usersoshibaappdatalocalemppip-omripn-build, content-type: text/html; charset=utf-8); cannot detect archive format
Cannot determine archive format of c:usersoshibaappdatalocalemppip-omripn-build

I had a 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 a 32-bit Python version. But still I'm getting the same error. Though my Python 32-bit version is working fine.

I tried "pip install numpy", but that gave me the following error at the end -

C:Python27libdistutilsdist.py:267: UserWarning: Unknown distribution option: 'define_macros'

  warnings.warn(msg)

error: Unable to find vcvarsall.bat

----------------------------------------
Command "C:Python27python.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('
', '
'),__file__, 'exec'))" install --record c:usersoshibaappdatalocalemppip-x_6llm-recordinstall-record.txt --single-version-externally-managed --compile" failed with error code 1 in c:usersoshibaappdatalocalemppip-build-hdhqex
umpy

What might I be doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Then there are basically two ways to install Python packages:

  • 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.

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

For both, you need to check that the binary has been strictly compiled for your Python architecture (32 or 64 bits) and version.

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.

Example: 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.

Official distributions of Python use Microsoft Visual C++ for Microsoft Windows packages. So you will need to install this compiler in this case.

How to find proper version of 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:

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.

How to install 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.

Here are some reference links:

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.

They have been reported in several questions already:

As a solution, you can check this link: 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.

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.0VCvcvarsall.bat) to set up the proper environment variables so that the execution of distutils doesn't fail when trying to compile a package.

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.

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 FilesMicrosoft SDKsWindowsv7.1BinSetEnv.cmd" /x64.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...