首页 > 代码库 > 使用Python的pip命令安装numpy

使用Python的pip命令安装numpy

NumPy 是一个基础科学的计算包。很多的科学计算特别是向量处理的时候会用到。 因为用到了,而且安装的过程中出现了一些问题,所以记录下来,便于备查。

 

平台: windows 8.1 64位

Python: 2.7.9 32位(已包含pip命令)

无Microsoft的VS环境

 

打开powershell,(事先将Python以及Python的Script的目录导入环境变量PATH中),输入

pip install numpy

 

会出现:

Unable to find vcvarsall.bat

 的错误消息。

 

原因是:the Python package being installed contains the source code for a native extension module (.pyd), but does not have a pre-built copy of the module. The Python packages highlighted at pythonwheels.com have already been updated by their developers to include pre-built binaries, but many are still only available as source code.(来自微软官网)

我自己的理解是:要编译的代码中用到了C的代码,而这个包的代码发布的时候没有预先编译成Python的二进制文件,所以安装过程中需要使用C complier 编译这些模块。

所以,解决的方法就是安装 Microsoft Visual C++ Compiler for Python 2.7 。

安装成功之后,继续执行

pip install numpy

 

出现了下列的错误:

RuntimeError: Broken toolchain: cannot link a simple C program 

 

具体原因是为什么,我也没看懂,不知道为什么,但是找到了解决这个问题的方法:

在%PythonHome%Lib\distutils下,(默认是C:\Python27\Lib\distutils)修改msvc9compiler.py文件:

# embed the manifest # XXX - this is somewhat fragile - if mt.exe fails, distutils # will still consider the DLL up-to-date, but it will not have a # manifest.  Maybe we should link to a temp file?  OTOH, that # implies a build environment error that shouldn‘t go undetected. # change by YQ to install numpy in 2015/1/16 mfinfo = None  #<===# mfinfo = self.manifest_get_embed_info(target_desc, ld_args)

 

找到这些代码,应该在648行左右,将箭头右方的代码更改成箭头左方的代码。

解决方法的网址:http://numpy-discussion.10968.n7.nabble.com/Compiling-NumPy-on-Windows-for-Python-3-3-with-MSVC-2010-td32356.html

然后再运行:

pip install numpy

 

终于OK了。

 

 

附测试程序:

# import the necessary packagesimport numpy as npdef chi2_distance(histA, histB, eps = 1e-10):        # compute the chi-squared distance    d = 0.5 * np.sum([((a - b) ** 2) / (a + b + eps)        for (a, b) in zip(histA, histB)])    Sum = 0;    for (a,b) in zip(histA, histB):        E  = ((a - b) ** 2) / (a + b + eps)        Sum += E        print Sum/2    # return the chi-squared distance    return dd = chi2_distance([0,1,2,3,4],[4,3,2,1,0])# print d    

 

 

 

 

 

 

 

 

 

使用Python的pip命令安装numpy