Building Python 2.7.11 with Visual Studio 2015
Official Python 2.7 is compiled with MSC v.1500
(Visual Studio 2008). Maya 2018 through 2020 use slightly different build of cPython:
For reference
Visual C++ version | _MSC_VER |
---|---|
Visual C++ 2005 (8.0) | 1400 |
Visual C++ 2008 (9.0) | 1500 |
Visual C++ 2010 (10.0) | 1600 |
Visual C++ 2012 (11.0) | 1700 |
Visual C++ 2013 (12.0) | 1800 |
Visual C++ 2015 (14.0) | 1900 |
Important notes
Before going further, I want to make sure it is clear this isn’t something you should be doing unless absolutely necessary. Python 2.7 was never officially intended to be compiled (on windows) with anything other than VS2008 and VS2010. Full functionality of the build produced the way described here cannot be guaranteed. In fact, if you run the regression tests, they will fail. Latter in the post I point to alternatives that might better suite your needs but if you need this exact version (as I do), than follow along and keep in mind the caveats.
Prerequisites
- svn - needed for fetching externals, make sure you enable command line tools (perhaps disable additional icons if you don’t need those)
- git - needed for fetching python source
- visual studio 2015 - at this point, it might be a bit hard to get older versions of VS but at least the Community Edition is enough
Build steps and shenanigans
Make sure you have git installed, than crate a build folder wherever you whish and run:
git clone https://github.com/python/cpython.git --branch v2.7.11
Patching the source
To force python to compile, we need to change a few things.
I would’ve preferred to simply fork the source and patch it to save myself (and others, possibly) some time but we have to patch dependencies as well so at that point I didn’t see much value in doing so.
In Modules\posixmodule.c
replace L569-605 to:
/* This function emulates what the windows CRT does to validate file handles */
int
_PyVerify_fd(int fd)
{
//a call to _get_osfhandle with invalid fd sets errno to EBADF
if (_get_osfhandle(fd) == INVALID_HANDLE_VALUE)
return 0;
else
return 1;
}
In Modules\timemodule.c
replace L708-727 to:
#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
tzset();
#ifdef PYOS_OS2
PyModule_AddIntConstant(m, "timezone", _timezone);
#else /* !PYOS_OS2 */
PyModule_AddIntConstant(m, "timezone", _timezone);
#endif /* PYOS_OS2 */
#ifdef HAVE_ALTZONE
PyModule_AddIntConstant(m, "altzone", altzone);
#else
#ifdef PYOS_OS2
PyModule_AddIntConstant(m, "altzone", _timezone-3600);
#else /* !PYOS_OS2 */
PyModule_AddIntConstant(m, "altzone", _timezone-3600);
#endif /* PYOS_OS2 */
#endif
PyModule_AddIntConstant(m, "daylight", _daylight);
PyModule_AddObject(m, "tzname",
Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
In PCbuild\tcltk.props
replace L41 to:
<BuildDirTop>$(BuildDirTop)_VC13</BuildDirTop>
Getting externals
Run PCbuild\get_externals.bat
(if you don’t have svn installed, this will fail).
Patching externals
In following files:
externals\tcl-8.5.15.0\generic\regguts.h
externals\tcl-8.5.15.0\generic\regcomp.c
externals\tcl-8.5.15.0\generic\regexec.c
…replace all occurrences ofINFINITY
withDUPINF
(make sure you match the case and whole word!).
In externals\tcl-8.5.15.0\win\tclWinPort.h
after line 368 (#endif /* __BORLANDC__ */
) add:
#if defined(_MSC_VER) && _MSC_VER >= 1900
#define timezone _timezone
#define environ _environ
#endif /* defined(_MSC_VER) && _MSC_VER >= 1900 */
In externals\tk-8.5.15.0\win\ttkWinXPTheme.c
replace L28-32 with:
#include <vssym32.h>
Finally build it
Open pcbuild.sln
with Visual Studio 2015. You will be warned that projects use an older version of Visual C++ compiler and asked if you’d want them to be upgraded:
Click OK
and wait. Change configuration and platform to Release x64:
Select Solution in Explorer, right click and Build Solution
.
Packaging
And we’re done! Right? No, not exactly.
The binaries will be in PCbuild\amd64
. You now need to install official release of python 2.7.11 and replace the the binaries with the ones you just built. They will need to be placed in root
and DLLs
folders respectively.
Alternatives
If you just need 2.7 built with newer Visual Studio and don’t mind the exact patch version, I strongly recommend checking out Kovid Goyal’s fork on GitHub (at the moment I’m writing this it will build 2.7.16 with VS2017). Please note that I, personally, did not use it, but looking at the commits it’s obvious this fork handles much more than described here – it actually implements many fixes and with significantly better expertise than my own.
Alternatively, you may use Gerson Kurz’s builds of 2.7.10 or follow instructions on the linked page (which, again, I haven’t used since I needed exact version of the interpreter that maya 2018 comes with).