windows平台使用Microsoft Visual C++ Compiler for Python 2.7编译python扩展
在windows平台上安装python c extension的扩展包是件很痛苦的事情,一般通过安装vc/vs系列来编译C扩展,不过安装包都比较大。或者通过mingw编译,不过有时会在兼容性上出现点问题。
有个好消息就是微软为Python提供了专用的编译器Microsoft Visual C++ Compiler for Python 2.7(包含32位和64位) 下载地址: http://aka.ms/vcpython27
提示:在此感谢@ThunderEX的提醒,setuptools 6.0及后续版本可以自动识别Microsoft Visual C++ Compiler for Python 2.7了,赶紧升级吧。如果不能升级,请参考下面的操作步骤。
1.下载完成并安装。以本机为例,安装完成后的路径为:
C:\Users\Administrator\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0
2.修改python安装目录下Lib\distutils\msvc9compiler.py文件(如有必要可能msvccompiler.py文件也需要做相应更改,视系统而定),找到get_build_version方法直接return 9.0
def get_build_version():
"""Return the version of MSVC that was used to build Python. For Python 2.3 and up, the version number is included in
sys.version. For earlier versions, assume the compiler is MSVC 6.
"""
return 9.0
prefix = "MSC v."
i = sys.version.find(prefix)
if i == -1:
return 6
i = i + len(prefix)
s, rest = sys.version[i:].split(" ", 1)
majorVersion = int(s[:-2]) - 6
minorVersion = int(s[2:3]) / 10.0
# I don't think paths are affected by minor version in version 6
if majorVersion == 6:
minorVersion = 0
if majorVersion >= 6:
return majorVersion + minorVersion
# else we don't know what version of the compiler this is
return None
然后再找到find_vcvarsall方法直接返回vcvarsall.bat的路径(以自己机器安装后的路径为准)
def find_vcvarsall(version):
"""Find the vcvarsall.bat file At first it tries to find the productdir of VS 2008 in the registry. If
that fails it falls back to the VS90COMNTOOLS env var.
"""
return r'C:\Users\Administrator\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat'
vsbase = VS_BASE % version
try:
productdir = Reg.get_value(r"%s\Setup\VC" % vsbase,
"productdir")
except KeyError:
productdir = None # trying Express edition
if productdir is None:
vsbase = VSEXPRESS_BASE % version
try:
productdir = Reg.get_value(r"%s\Setup\VC" % vsbase,
"productdir")
except KeyError:
productdir = None
log.debug("Unable to find productdir in registry") if not productdir or not os.path.isdir(productdir):
toolskey = "VS%0.f0COMNTOOLS" % version
toolsdir = os.environ.get(toolskey, None) if toolsdir and os.path.isdir(toolsdir):
productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
productdir = os.path.abspath(productdir)
if not os.path.isdir(productdir):
log.debug("%s is not a valid directory" % productdir)
return None
else:
log.debug("Env var %s is not set or invalid" % toolskey)
if not productdir:
log.debug("No productdir found")
return None
vcvarsall = os.path.join(productdir, "vcvarsall.bat")
if os.path.isfile(vcvarsall):
return vcvarsall
log.debug("Unable to find vcvarsall.bat")
return None
3.上述完成之后就可以在windwos下正常编译python的C扩展。以pycrypto-2.6.1为例,执行如下命令
python setup.py install
当然也可以建立一个windows的二进制包
python setup.py bdist_wininst
windows平台使用Microsoft Visual C++ Compiler for Python 2.7编译python扩展的更多相关文章
- Python Microsoft Visual C++ Compiler Package for Python 2.7
错误描述: 在从源代码安装Python模块时遇到此错误.可是我明明从官网下载并安装了Microsoft Visual C++ Compiler Package for Python 2.7,且配置了环 ...
- Microsoft Visual C++ Compiler for Python 2.7
Extest.c文件:#include <stdio.h> #include <stdlib.h>#include <string.h>#include " ...
- Microsoft Visual C++ Compiler for Python
Visual C++ |CPython ------------------------------------------- 14.0(2015) |3.5 10.0(2010) |3.3, 3.4 ...
- Microsoft Visual C++ Compiler for Python 2.7真正下载地址
真正下载地址: http://origin.www.ms.akadns.NET/en-us/download/details.aspx?id=44266 那个微软地址好像不可用.
- windows平台 python生成 pyd文件
Python的文件类型介绍: .py python的源代码文件 .pyc Python源代码import后,编译生成的字节码 .pyo Python源代码编译优化生成的字节 ...
- 【Python】安装python包时遇到"error: Microsoft Visual C++ 9.0 is required"的简答
简答 在Windows下用pip安装Scrapy报如下错误, error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall ...
- windows平台快速安装 matplotlib
版本:Python2.7.9 Pip 1.5.6 如果没有安装VC2008以上版本,就需要工具Microsoft Visual C++ Compiler for Python 2.7 http://w ...
- 安装python包时遇到"error: Microsoft Visual C++ 9.0 is required"的简答
简答 在Windows下用pip安装Scrapy报如下错误, error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall ...
- 解决wordcloud的一个error:Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
环境: 操作系统:Windows 7 64位 语言:Python 2.7.13 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:17:26) w ...
随机推荐
- 选址问题lingo求解
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang model : sets : H/h1..h2/:x , y , e ; L/l1..l6/: a ...
- 如何在Latex上裁减图片
在Latex的使用过程中,很多人需要载入一些具有一定白边或者边框的图片.特别是用matlab生成的很多图片.大部分人的做法是通过使用pdf工具去裁减.这样做很麻烦,并且对于一些批量的,大小相同的图片而 ...
- apache window环境下本地配置虚拟主机
1.在httpd.conf中去掉如下注释: # Virtual hostsInclude conf/extra/httpd-vhosts.conf 2.在 httpd-vhosts.conf添加内容 ...
- 解决kibana 4 关于响应时间的问题
"message" => " 10.252.142.174 [12/Sep/2016:16:43:47 +0800] \"GET /resources/j ...
- 在Struts2中集成Spring详细讲解
Spring的官方定义是:一个轻量级的IoC和Aop容器框架,它使用了一种叫做依赖注入的技术. 所谓依赖注入,就是指将创建对象以及协议依赖对象之间合作的责任从对象自身中转移到"工厂" ...
- Sqrt(x) 解答
Question Implement int sqrt(int x). Compute and return the square root of x. Solution 1 -- O(log n) ...
- SSH方式登录github出现Permission denied (publickey)
今天在公司上传了代码,回到家pull,结果竟然出现了“Permission denied (publickey)“这种东西.第一反应是key不对,可是上次明明用key登录过,不可能不对啊,难道是文件被 ...
- Angular学习笔记(2)——TODO小应用
Angular学习笔记(2)--TODO小应用 1. 写在前面 之前我们跑了Angular的Hello World,你是不是对它有点感觉了呢?这一篇将结合一个TODO程序来继续学习Angular的用法 ...
- 用CSS/CSS3 实现 水平居中和垂直居中的完整攻略
水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: .parent { text-align:center; } 水 ...
- [think in java]知识点学习
java中 全部数值都有正负号,不存在无符号整数. java中的基本类型存储在堆栈中. 其它对象存储在堆中. java确保数组会被初始化,并且不能在它的范围之外被訪问. 下面代码在c和c++中是合法的 ...