compiling python extension modules with the Windows SDK

Sunday 08 June 2014, 16:04:00 | python

Instead of using the Mingw-toolchain (f.ex. see →Installing PyCrypto on Windows (or how to build installers for libraries with C-modules)) it is also possible to just use the official Microsoft SDK compiler.

I've installed the Windows 7 sdk from Microsoft

Make sure you run the following from the "Windows SDK 7.1 command prompt":

X:\> setenv /release /x86 /win7

Then running the installation of a library that has a C extension module almost succeeds:

X:\>c:\Python27\Scripts\pip.exe install cytoolz
Downloading/unpacking cytoolz
  ...
  ...
running build_ext

building 'cytoolz.dicttoolz' extension

error: Unable to find vcvarsall.bat

(!) Turns out you have to tell Python's setuptools to use the SDK compiler instead of looking for the full Visual Studio C++ compiler:

X:\>set MSSDK=1
X:\>set DISTUTILS_USE_SDK=1

After setting these environment variables, things will compile fine:

X:\>c:\Python27\Scripts\pip.exe install cytoolz
   ...
   ...
  Running setup.py install for cytoolz
    building 'cytoolz.dicttoolz' extension
    C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Bin\cl.exe /c /nologo
 /Ox /MD /W3 /GS- /DNDEBUG -Ic:\Python27\include -Ic:\Python27\PC /Tccytoolz/dic
ttoolz.c /Fobuild\temp.win32-2.7\Release\cytoolz/dicttoolz.obj
    dicttoolz.c
  ...
Successfully installed cytoolz