Posted on May 31, 2008 at 2:16 pm

Building Python Extensions on Win32 with MS VC++ 2008 Express

These days, I’m mostly working on linux and I haven’t had to use the Microsoft dev tools for a long time. I’m using Vista on my new laptop. Yesterday, I had to install pycrypto from source on Windows. The only compiler I had was MinGW, which doesn’t quite cut it with distutils. I figured that I’d have an easier time with the free Visual C++ 2008 Express Edition compiler. Apparently, distutils on Python 2.5.2 doesn’t like this compiler either.
Here’s what I had to do to get distutils to work with it.
First, patch distutils.
File: C:\Python25\Lib\site-packages\msvccompiler.py
One of the things I observed in this file was that if
the environment variables DISTUTILS_USE_SDK and MSSdk
are set and if distutils can find “cl.exe” in the system path, it assumes that everything is set up.
To quote from a comment in the source,

# Assume that the SDK set up everything alright; don’t try to be
# smarter

Find the method load_macros in the MacroExpander class.
Replace the following lines inside the try block

if version > 7.0:
     self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv1.1")
else:
     self.set_macro("FrameworkSDKDir", net, "sdkinstallroot")</pre>

With:

if version > 7.09:
     pass
elif version > 7.0:
     self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv1.1")
else:
     self.set_macro("FrameworkSDKDir", net, "sdkinstallroot")

Now launch a command prompt window and run the following commands.

C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat
SET DISTUTILS_USE_SDK=1
SET MSSdk=1

Now you can run “setup.py build” and it works just fine.
The compiler spits out a couple of warnings about the option ‘GX’ being deprecated, but thats just an innocuous warning.

Tags:, , ,

5 Responses to “Building Python Extensions on Win32 with MS VC++ 2008 Express”

  1. Miroslav Sabljic on September 4th, 2008 at 2:39 PM says:

    Thanks for this! It helped me a lot with building PIL on Windows using VC9 compiler.

  2. irha on April 24th, 2009 at 12:23 AM says:

    Is this solution expected to work with python 2.4.x also? I had mingw setup and it worked for a few extensions, but when I got build errors on guppy extension, I followed your steps, but the build gave this error:

    error: Python was built with version 7.1 of Visual Studio, and extensions need to be built with the same version of the compiler, but it isn’t installed.

    Appreciate any help.

  3. Jeethu on May 1st, 2009 at 11:33 AM says:

    I haven’t tried it with Python 2.4. Seems like you’ll also have to compile the python core with MSVC 8.0, You might want to download the source and try compiling it.

  4. iPAS on May 7th, 2009 at 9:13 AM says:

    Thank you vary much. This solution is hard to found on internet.

  5. Tagz | "Jeethu’s Blog » Building Python Extensions on Win32 with MS VC++ 2008 Express" | Comments on May 16th, 2009 at 1:30 PM says:

    [...] [upmod] [downmod] Jeethu’s Blog » Building Python Extensions on Win32 with MS VC++ 2008 Express (jeethurao.com) 1 points posted 10 months, 1 week ago by jeethu tags python windows [...]

Leave a Reply