Windows platform compiles Python extensions using Microsoft Visual C + + Compiler for Python 2.7

Source: Internet
Author: User

Installing the Python C Extension extension package on the Windows platform is a pain, typically by installing the Vc/vs series to compile the C extension, but the installation package is larger. or compile with MinGW, but sometimes there are some problems with compatibility.

The good news is that Microsoft provided Python with a dedicated compiler for Microsoft Visual C + + Compiler for Python 2.7 (with 32-bit and 64-bit): http://aka.ms/vcpython27

1. Download complete and install the path on my machine to complete the installation:

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

2. Modify the Python installation directory under the lib\distutils\msvc9compiler.py file to find the Get_build_version method directly return 9.0

Def get_build_version (): "" "Return the version of MSVC that is    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 is affected B Y minor version in version 6    if MajorVersion = = 6:        minorversion = 0    if MajorVersion >= 6:        return MA Jorversion + minorversion    # Else we don ' t know what version of the compiler this is    return None

Then find the Find_vcvarsall method to return the path of the Vcvarsall.bat directly (whichever is the path after the installation of your own machine)

def find_vcvarsall (version): "" "Find the Vcvarsall.bat file at first it tries to find the productdir of VS. In T He 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.f0    Comntools "% 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 to 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. After the completion of the above, you can normally compile Python's C extension under Windwos. Take pycrypto-2.6.1 as an example, execute the following command

Python setup.py Install

Of course, you can also create a Windows binary package

Python setup.py bdist_wininst

Windows platform compiles Python extensions using Microsoft Visual C + + Compiler for Python 2.7

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.