Python 2.7 was released on Jul. 3rd, and for many developers and end users that means recompiling a bunch of libraries. I made this quick and dirty guide to show you how to use Visual Studio 2010 Express to compile your libraries without having to build another mingw32 toolchain or search for an old version of Visual Studio.

Before you start, have a look at Christoph Gohlke’s Python Extension Packages for Windows. You may find a binary package there and not need to compile it at all.

http://www.lfd.uci.edu/~gohlke/pythonlibs/

If you didn’t find what you need, don’t stress. Just grab Visual Studio 2010 Express if you don’t already have it and follow along.

First, msvc9compiler is a mess. If you try to run setup.py build, you’ll be greeted with an error:

error: Unable to find vcvarsall.bat

Instead of messing around with huge edits that you find on mailing lists or Stack Overflow, just open up

C:\Python27\Lib\distutils\msvc9compiler.py

and replace line 243 with this:

        toolskey = "VS100COMNTOOLS"

since you have it open, you can avoid manifest errors caused by unruly setup.py scripts by adding

            ld_args.append('/MANIFEST')

after line 647. This is optional, and not a very elegant solution…

Now you need to set an environmental variable in Windows so that Python can find Visual Studio.

Name = VS100COMNTOOLS
Value = C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\

Typically, you just need to add it via the “Advanced System Settings” link in Control Panel\System and Security\System.

Alternatively, you can set it globally in a command prompt with this command:

set VS100COMNTOOLS="C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\"

or per user with setx instead.

If you happen to use virtualenv, you can add it to your env\Scripts\activate.bat script.

A final note: If you intend to redistribute you packages, you should be aware that the end user will likely need msvcr100.dll (C:\Windows\SysWOW64 on Windows 7 x64) to load any compiled libraries you have included. Pointing them to the appropriate Microsoft Visual C++ 2010 Redistributable Package should suffice:

http://www.microsoft.com/downloads/en/results.aspx?pocId=&freetext=Microsoft%20Visual%20C%2b%2b%202010%20Redistributable%20Package&DisplayLang=en

7 Responses to “Compile Python 2.7 Packages With Visual Studio 2010 Express”

  1. Be careful if you do this. Python 2.6 and 2.7 from python.org are built with Visual Studio 2008 compilers. You will need to link with the same CRT (msvcr90.dll) as Python.

    Visual Studio 2010 Express links with the wrong CRT version: msvcr100.dll.

    If you do this, you must also re-build Python with Visual Studio 2010 Express. You cannot use the standard Python binary installer for Windows. Nor can you use any C/C++ extensions built with a different compiler than Visual Studio 2010 (Express).

    Courtesy of Microsoft, we have C runtime DLL hell on Windows, beginning with the release of Visual Studio 2003. Very inconvenient indeed. :(

    • why we always complains against M$.

      it’s python’s fault too, especially when we build python extensions. python shouldn’t restrict us to us other compilers.

  2. To get the correct C/C++ compiler for official Python 2.6 and 2.7 releases, download the Windows 7 SDK for .NET framework version 3.5.

    ASFAIK it cannot be used with VS2010 Express.

  3. Sturla Molden :
    If you do this, you must also re-build Python with Visual Studio 2010 Express.

    I haven’t had a problem yet. I’ve been using standard Python binary releases with various official packages compiled on mingw32, vs2008, along with my own compiled with vs2010 pro and express.

    Sturla Molden :

    To get the correct C/C++ compiler for official Python 2.6 and 2.7 releases, download the Windows 7 SDK for .NET framework version 3.5.

    ASFAIK it cannot be used with VS2010 Express.

    Windows Platform SDK compilers and core files are included in VS2010 Express.

  4. VS 2010 Express links with incompatible C and C++ runtime DLLs. This should be well known if you read Python mailing lists or comp.lang.python. You cannot compile extensions for Python with any version of Microsoft’s compilers. They will compile and build, but can/will randomly segfault on runtime.

    In particular, VS 2010, VS 2010 Express, or Windows 7 SDK for .NET version 4 cannot be used with Python 2.6 or 2.7. You must use the compiler from the link I provided, or get VS 2008 from an MSDN subscription (it only costs $1200…)

  5. fuzion: thanks for these tips. I have codified your suggestions in a function that will patch the msvc9compiler module here: http://bitbucket.org/jaraco/jaraco.develop/src/f6e937d98e7f/jaraco/develop/msvc.py

    This function is available in jaraco.develop 1.2. Feel free to steal the function, or use it directly. What I do is
    easy_install jaraco.develop>=1.2
    Then add the following to my sitecustomize.py or usercustomize.py:

    from jaraco.develop import msvc
    msvc.patch_msvc9compiler_module()

    Then, regardless how I’m installing an extension module, the patch will be engaged, and the VS2010 will be used. All the while, the Python source remains unchanged. Personally, I prefer this approach to patching the Python lib source, because remembering that configuration and keeping it consistent across machines is harder.

    Thanks for the tips.

  6. Hello

    If you have a feature request for MSVC 10 support, or a bug to report like the file not found one, you’re very welcome to report them on bugs.python.org (or to add comments to existing reports). We’re only a handful of developers fixing distutils bugs, and I for one lack Windows knowledge, but we do address bugs. Thanks in advance.

    Regards

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

© 2010 nukeitdotorg