Using setuptools with the Microsoft Visual C++ Compiler for Python 2.7

By | 19/11/2014

Just a couple of days ago I faced the problem that I wanted to write a Python 2.7 module in C. Why in C, you ask? It had nothing to do with number crunching, or performance optimization. It was merely just to create Python bindings for Valve’s Steamworks SDK. This is strictly speaking not a super difficult task in this day and age, except that Python 2.7 uses a fairly old compiler on Windows that is pretty much unavailable nowadays – Microsoft Visual C++ 2008. Fortunately Microsoft seems to have been made aware of this problem. As I found out after a few Google searches, they are now providing an unsupported package containing the required compiler and libraries. Nice! At least that was my thought until I ran into some issues getting it to work with setuptools. Since the package is not supported by Microsoft, I want to share what I had to do to get it to work. Note that I installed the compiler package for all users on my computer, but the steps should be the same for the “current user only” installation – it’s just a different installation directory after all.

So, what were the problems? Well, first of all, the installed compiler does not add any registry values that setuptools’ heuristics for detecting MSVC could find. Not really a big deal because the heuristics fall back to an environment variable – in this case it should be VS90COMNTOOLS. However, as the second issue, the compiler package’s setup routine does not create this environment variable. This should also be a trivial thing to add. Except, thirdly, the compiler package has a different installation path and directory layout from the normal Visual Studio distributions. Thus simply copy/pasting the environment variable for a different visual studio version is not enough, it is needs to be slightly adjusted: Instead of pointing towards [Visual Studio Path]\Common7\Tools\ I had to point it to [Visual Studio Path]\VC\bin. This gets us far enough for setuptools to find the location where it expects the vcvarsall.bat file. All good, right? No! Because, last but not least, the expected vcvarsall.bat file does not exist in this distribution. I had to copy one from my [Visual Studio 2013 Path]\VC\ directory to the VC directory of the Visual C++ compiler for python installation folder, and do a manual edit in this copied batch file: The line with set VisualStudioVersion=12.0 needs to be changed so that the version number is 9.0. And that finally got me going. Note: there is a vcvarsall.bat file in the root directory of the compiler installation. Copying this into the VC subfolder will not work unless you modify all the paths in there. It’s probably an option as well, but requires more changes than just adjusting a version number.

3 thoughts on “Using setuptools with the Microsoft Visual C++ Compiler for Python 2.7

  1. Gary WalkerGary Walker

    Ugh, that sounds rough. Hopefully this will help the next poor soul with the same problem 🙂

  2. Thomas DählingThomas Dähling

    Yeah, I didn’t expect to hit these speed bumps. What saved me was that I ran into a similar issue before at CCP and thus knew that setuptools rely on certain heuristics, and that I knew how a lot of the details have to be. Still, unpleasant. I should probably add the setuptools error message in this post so that people googling for it will find this a lot easier.

Leave a Reply

Your email address will not be published. Required fields are marked *