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

1.下载完成并安装 安装完成在我的机器上的路径为:

C:\Users\Administrator\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0

2.修改python安装目录下Lib\distutils\msvc9compiler.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
时间: 2024-10-12 21:21:52

windows平台使用Microsoft Visual C++ Compiler for Python 2.7编译python扩展的相关文章

Microsoft Visual C++ Compiler for Python 2.7

Extest.c文件:#include <stdio.h> #include <stdlib.h>#include <string.h>#include "Python.h" #define BUFSIZE 10 int fac(int n) { if (n < 2) return 1; return n * fac(n - 1);} char *reverse(char *s) { register char t; char *p = s;

Microsoft Visual C++ Compiler for Python

Visual C++ |CPython ------------------------------------------- 14.0(2015) |3.5 10.0(2010) |3.3, 3.4 9.0(2008) |2.6, 2.7, 3.0, 3.1, 3.2

error: Microsoft Visual C++ 14.0 is required when installing python package

Binary install it the simple way!I can't believe no one has suggested this already - use the binary-only option for pip. For example, for mysqlclient: pip install --only-binary :all: mysqlclientMany packages don't create a build for every single rele

Windows下pip安装包报错:error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat)

在Windows7x64下使用pip安装包的时候提示报错: error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat). Get it from http://aka.ms/vcpython27 原因:windows下使用pip安装包的时候需要机器装有vs2008,VS2012还不行,如果不想装VS2008的话,可以安装一个Micorsoft Visual C++ Compiler for Python 2

【Python】安装python包时遇到&quot;error: Microsoft Visual C++ 9.0 is required&quot;的简答

简答 在Windows下用pip安装Scrapy报如下错误, error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat). Get it from http://aka.ms/vcpython27 打开http://aka.ms/vcpython27会跳转到http://www.microsoft.com/en-us/download/confirmation.aspx?id=44266 将安装包(VCFo

Microsoft Visual Studio 2015 python 安装 mysql-python 出错解决

Microsoft Visual Studio 2015 安装 python 连接包 mysql-python出错   第一种 pip安装方式 安装Microsoft Visual C++ Compiler for Python 2.7 安装后错误改变   无用 _mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory   暂时未解决 第二种方式eas

windows平台下基于VisualStudio的Clang安装和配置

LLVM 是一个开源的编译器架构,它已经被成功应用到多个应用领域.Clang是 LLVM 的一个编译器前端,它目前支持 C, C++, Objective-C 以及 Objective-C++ 等编程语言.Clang 对源程序进行词法分析和语义分析,并将分析结果转换为 AST ( 抽象语法树 ) ,最后使用 LLVM 作为后端代码的生成器. Clang 的开发目标是提供一个可以替代 GCC 的前端编译器.与 GCC 相比,Clang 是一个重新设计的编译器前端,具有一系列优点,例如模块化,代码简

Python error: Microsoft Visual C++ 9.0 is required 解决方案

换了新电脑,在使用python2.7 pip 安装ipython时,报错了 error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27 解决方法: 下载 Microsoft Visual C++ Compiler for Python 2.7, 安装即可 下载链接:http://www.microsoft.com/en-us/download/confirmation.aspx?id=4426

Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat 解决办法

问题描述 安装 Python的MySQL驱动时时出现这个错误: Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat 环境 python 2.7.10 mysql 5.6 django 1.8.3 解决办法 下载  Microsoft Visual C++ Compiler for Python 2.7, 安装一下问题解决 下载链接: http://www.microsoft.com/en-us/download/co