Sunday, August 16, 2015

Compiling Python extensions on Windows

source: https://blog.ionelmc.ro/2014/12/21/compiling-python-extensions-on-windows/

Using Windows is difficult...

Extensions usually need to be built with the same compiler version as the interpreter. This is because each compiler version uses a different and incompatible CRT, until VS 2015 that is.

If you're missing the required compiler you usually get a vague error: Unable to find vcvarsall.bat.


For Python 3.4

This is the python version I currently use.

In order to get it to work you need to jump through these hoops:

1. Install Visual C++ 2010 Express

2. Before installing the Windows SDK v7.1 (WARNING!):
  • Do not install Microsoft Visual Studio 2010 Service Pack 1 yet. If you did then you have to reinstall everything. Uninstalling the Service Pack removes components so you have to reinstall Visual C++ 2010 Express again.
  • Remove all the Microsoft Visual C++ 2010 Redistributable packages from Control Panel\Programs and Features.

If you don't do those then the install is going to fail with an obscure "Fatal error during installation" error.


3. Install Windows SDK for Visual Studio 2010 (v7.1). This is required for 64bit extensions.

⚠ On Windows 10 the web installer gets confused:
Some Windows SDK components require the RTM .NET Framework 4. Setup detected a pre-release version of the .NET Framework 4.
You certainly don't need any of the .NET stuff so you can work around by getting the offline version of the SDK (ISO).

  • Make sure you download GRMSDKX_EN_DVD.iso (X means 64bit).
  • Windows has builtin mounting for ISOs. Just mount the ISO and run Setup\SDKSetup.exe instead of setup.exe.

4. Create a vcvars64.bat file in C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64 that contains:

CALL "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64


5. Now everything should work, go and try py -3 setup.py clean --all build_ext --force.

6. Install Microsoft Visual Studio 2010 Service Pack 1. This is optional, however, if you do this you also have to do the following too:


8. Remove all those SQL Server packages that you don't need from Control Panel\Programs and Features.


For Python 3.5

Get the Visual C++ 2015 Build Tools. It doesn't come with Visual Studio and all the useless cruft but make sure you select the Windows 8.1 SDK during the install.

Thank you so much, Ionel.