Sat, 05 Jan 2013 by Bolshevik in python | Comments

Some python extensions e.g. pycrypto are distributed as a source code and require compilation. It is a big problem to build them on Windows. There are two options to do this. It is better to use MinGW to build extensions which are very UNIX-tight. But to build 64-bit code one is forced to use MS SDK.

Some extentions may require Windows specific extensions: http://sourceforge.net/projects/pywin32/

Using MinGW

  1. Download the latest web installer at http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/ and install MinGW.
  2. Add PATH_TO_MINGW\bin to your PATH.
  3. In PYTHONPATH\Lib\distutils, create a file distutils.cfg and add these lines:
    [build]
    compiler=mingw32
    

The only limitation is that 32-bit extensions can be only built with MinGW. Therefore this won't work for x64 Python.

Using MS SDK

  1. It is possible to get the SDK by downloading Visual C++ Express or Windows SDK. Due to license problems it is suggested to use Windows SDK. Download web installer at http://www.microsoft.com/en-us/download/details.aspx?id=8279
  2. Run the installer select all the compilers except Itatinium architecture. You may also uncheck different tools and samples.
  3. Open command line and navigate to PATH_TO_VISUAL_STUDIO\VC\ e.g. _C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC_ and try to run

    vcvarsall.bat x86
    

    One may get the following error here. Further steps will help to resolve them. Setting environment for using Microsoft Visual Studio 2010 x86 tools.

    ERROR: Cannot determine the location of the VS Common Tools fold
    
  4. Open PATH_TO_VISUAL_STUDIO\VC\bin\vcvars32.bat.

  5. Add the following lines to the beginning of the file:

    @SET VSINSTALLDIR=PATH_TO_VISUAL_STUDIO
    @SET VCINSTALLDIR=PATH_TO_VISUAL_STUDIO\VC\
    @SET FrameworkDir32=c:\Windows\Microsoft.NET\Framework\
    @SET FrameworkVersion32=v4.0.30319
    @SET Framework35Version=v3.5
    

    Comment these lines:

    @call :GetVSCommonToolsDir
    @if "%VS100COMNTOOLS%"=="" goto error_no_VS100COMNTOOLSDIR
    
    @call "%VS100COMNTOOLS%VCVarsQueryRegistry.bat" 32bit No64bit
    
  6. Try to run

    vcvarsall.bat x86
    

    again. All must be ok.

  7. While compiling extensions one may get error that some header files (*.h) are missing to resolve the problem one should find SDK location. E.g. C:\Program Files\Microsoft SDKs\Windows\v7.1\. And add this to the beginning of the vcvars32.bat

    @SET WindowsSdkDir=PATH_TO_SDK\Windows\v7.1\
    
  8. Enable 64-bit compiling target. To do it create a file called vcvars64.bat at PATH_TO_VISUAL_STUDIO\VC\bin\amd64\ having this content

    @SET WindowsSdkDir=PATH_TO_SDK\Windows\v7.1
    call "%WindowsSdkDir%\Bin\SetEnv.Cmd" /x64 /Release
    
  9. Test the configuration by running

    >vcvarsall.bat x86
    Setting environment for using Microsoft Visual Studio 2010 x86 tools.
    >vcvarsall.bat amd64
    Setting SDK environment relative to C:\Program Files\Microsoft SDKs\Windows\v7.1\.
    Targeting Windows 7 x64 Release
    
  10. Python supports only version 9.0 of the Visual Studio and compiler tools, but the current version is 10.0 and will be updated in future. To tweak python navigate to PYTHONPATH\Lib\distutils\ open msvc9compiler.py, search for

    VERSION = get_build_version()
    

    and add

    VERSION = 10.0
    

    after initial detection. Search for

    ld_args.append('/MANIFESTFILE:' + temp_manifest)
    

    and add this after that line

    ld_args.append('/MANIFEST')
    
  11. Create a temporary virtual environment and switch to it. Test compilers by running

    pip install pycrypto
    

    or

    easy_install pycrypto
    

    One should see something like this:

    Downloading/unpacking pycrypto
      Running setup.py egg_info for package pycrypto
    Installing collected packages: pycrypto
      Running setup.py install for pycrypto
        building 'Crypto.Random.OSRNG.winrandom' extension
        c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\cl.exe /c         /nologo /Ox /MD /W3 /GS- /DNDEBUG -Isrc/ -Isrc/inc-msvc/ -IC:\Python26\include -IC:\Python26\PC /Tcsrc/winrand.c /Fobuild\temp.win32-2.6\Release\src/winrand.obj winrand.c
        c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\Python26\libs /LIBPATH:C:\Python26\PCbuild ws2_32.lib advapi32.lib /EXPORT:initwinrandom build\temp.win32-2.6\Release\src/winrand.obj /OUT:build\lib.win32-2.6\Crypto\Random\OSRNG\winrandom.pyd /IMPLIB:build\temp.win32-2.6\Release\src\winrandom.lib /MANIFESTFILE:build\temp.win32-2.6\Release\src\winrandom.pyd.manifest /MANIFEST
           Creating library build\temp.win32-2.6\Release\src\winrandom.lib and object build\temp.win32-2.6\Release\src\winrandom.exp
        C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\mt.exe -nologo -manifest build\temp.win32-2.6\Release\src\winrandom.pyd.manifest -outputresource:build\lib.win32-2.6\Crypto\Random\OSRNG\winrandom.pyd;2
    building 'Crypto.Hash._MD2' extension
    ...
    ...
    ...
    Successfully installed pycrypto
    Cleaning up...
    
  12. Delete temporary virtual environment and try installing other python module you need.

Comments

comments powered by Disqus

About

PHP web developer, python developer