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 ...
随机推荐
- 开心菜鸟系列学习笔记------------javascript(6)
一.作用域链 1)函数的生命周期: 函数的生命周期分为创建和激活阶段(调用时),让我们详细研究它. 作用域链与一个执行上下文相关,变量 ...
- find之exec和args
本来以为以前的差不多够用了.呵呵,看到很多高手用高技巧,心痒痒的觉得我自己还可以提升啊..哈哈哈. 这个实践起来之后,,SED,AWK也得深化一下,,,SHELL和PYTHON,作运维的两样都不能废. ...
- ScrollView中添加ListView
<p>.要点一:去除ListView的scrollBar,添加ScrollView的scrollBar:</p> <ScrollView android:layout_w ...
- NLS_LANG SIMPLIFIED CHINESE_CHINA.AL32UTF8 和american_america.AL32UTF8
oadb01:/home/oracle> echo $NLS_LANG SIMPLIFIED CHINESE_CHINA.AL32UTF8 oadb01:/home/oracle> sql ...
- highchart 实现mrtg
最近需要对流量图进行重构, <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- android学习--TabHost选项卡组件
TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...
- java RMI入门指南
感觉这篇文章不错,直接转了 RMI全称是Remote Method Invocation-远程方法调用,Java RMI在JDK1.1中实现的,其威力就体如今它强大的开发分布式网络应用的能力上,是纯J ...
- linux在文件打包和压缩
1. 打包和压缩文件 linux现在经常使用gzip和bzip2要压缩的文件.tar压缩文件. 经常使用的扩展: *.gz gzip压缩文件 *.bz2 bzip2压缩的文件 *.tar t ...
- Git 版本回退问题详解
版本回退 git log , git reset --hard xxxx回退到以前的版本 git reflog, git reset --hard xxx 回退到将来的版本 现在,你已经学会 ...
- html5 video播放不全屏
<video controls="controls" webkit-playsinline src="${page.videoUrl }" type=&q ...