I recently had the need to build a Python 2.7 debugging environment for my porting work of the angr platform to Windows. The problem I have run into is that there is not terribly good documentation on this subject. Not only was some of the official documentation from python misleading, but the links to the tools required were not easy to find. Here's how I built my debugging environment.

 

Note that I highly recommend you do this in a clean virtual machine. You will need to install quite a few things that will junk it up. Also, I'm writing this from the perspective that I have purchased copy of Visual Studio 2008 or 2010. When building python you actually want to marry up the version of the build environment as close as you can. For python version 2.7, this means Visual Studio 2008. This is because binaries are already linked against the msvcr90.dll where appropriate and using other versions may cause things to not work correctly. For my environment, I am using a clean Windows 7 x64 virtual machine for this process.

 

Here's the cheat sheet I've put together from their documentation as of February 2016.

 

Python Version Visual Studio Version
>= 3.5 Microsoft Visual Studio 2015
3.3/3.4 Microsoft Visual Studio 2010
<= 3.2 Microsoft Visual Studio 2008

 

Installing SVN

Let's start off by installing svn. This will be used by the builder script to dynamically pull in the required dependencies. The thing to make sure of at this step is that we're installing svn.exe into the PATH.

 

For this we can just use Tortoise SVN (https://tortoisesvn.net/downloads.html). On the install, be sure to select "command line client tools" from the install options. If you want to double check you're good to go, open a command prompt and type "svn --help".

 

Installing hg

For Python's own repository, they have chosen to use hg. For this, we will use Tortoise HG (http://tortoisehg.bitbucket.org/). Installation is strait forward. "hg help" will confirm that everything is working.

 

Copying down Python Development Files

For this, we'll use hg that we just installed. The following will create a cpython directory in C:

  1. Open a command prompt
  2. cd to the root
    1. cd \
  3. Clone their repo
    1. hg clone https://hg.python.org/cpython
  4. Check out Python 2.7
    1. cd cpython
    2. hg update 2.7

 

Install 7-zip

We'll be needing to extract an ISO, and 7-zip is a good utility. Find it here: http://www.7-zip.org/download.html

 

Install Visual Studio 2008 Express

Note, I'm using the express edition since I do not have the full edition of this particular version. If you have the Professional Edition then you should be able to install that and skip the rest of the Visual Studio/SDK installs. The point here is that while we need the Visual Studio installs, we also need x64 compilation support that doesn't come by default for the Express Edition.

 

The installer iso can be found here: https://go.microsoft.com/fwlink/?LinkId=104679

 

There are various solutions for mounting an iso file. I think on Windows 8+ you can do it directly. If you're on Win 7, use the 7-zip application as above to open it and copy out the files, then just run the setup executable.

 

When installing, choose the "Visual C++ Express" edition. Note: There's no need to install any of the extras (i.e.: SilverLight, SQLServerExpress, or MSDN Documentation).

 

Install Visual Studio 2010 Express

The version for this isn't really important. The important part is that the build system for python requires a newer version of msbuild than VS2008 has. Note that I attempted to use 2015, but there is a known issue at the moment where the compiling doesn't work the same and is causing problems (www.paraview.org/Bug/view.php?id=15534).

 

  1. Install .NET 4 framework
    1. https://www.microsoft.com/en-us/download/details.aspx?id=17851
  2. Install Visual Studio 2010
    1. http://download.microsoft.com/download/1/E/5/1E5F1C0A-0D5B-426A-A603-1798B951DDAE/VS2010Express1.iso
  3. Install Visual Studio 2010 SP 1
    1. https://www.microsoft.com/en-us/download/details.aspx?id=23691

 

Windows 7 SDK

To allow for building of the x64 python executable, we need x64 capable compilers. If you had the Professional version of Visual Studio you wouldn't need this step. Our plan here is to install the Windows 7 SDK which comes with the x64 compiler. In this way, we're basically going to be using 3 separate products in conjunction to successfully compile Python.

 

The installer is here: https://www.microsoft.com/en-us/download/details.aspx?id=8279

 

NOTE: I've been running into a problem where the x64 compilers don't actually install. This is a known issue and should be fixed by running the following installer AFTER the main installer: https://www.microsoft.com/en-us/download/details.aspx?id=4422

 

Compile Python 2.7 Debug

We're finally ready to do the compiling. Here you get to make a choice of compiling the debug version for 32 or 64bit. Using Visual Studio 2008 Express, the 32-bit compile will have the least problems integrating with existing packages since you can link it to the right DLL. Compiling for 64-bit will give you a warning about possible problems.

 

  • Open the build command prompt
    • start -> Microsoft Visual Studio 2010 Express -> Visual Studio Command Prompt (2010)
  • cd to the build directory
    • cd \cpython
  • 32-bit Build
    • PCbuild\build.bat -e -d -p Win32 "/p:PlatformToolset=v90"
  • 64-bit Build
    • PCbuild\build.bat -e -d -p x64 "/p:PlatformToolset=Windows7.1SDK"

 

Copy To New Directory

It's easiest and cleanest to use this if you copy the files you need to a new directory. We'll also set up a virtual environment system now where you can re-install/build those packages you need.

 

Create the directory structure as follows:

  • 32-bit
    • xcopy C:\cpython\PCBuild\python_d.exe \Python27_debug\.
    • xcopy /I C:\cpython\PCBuild\*.pyd \Python27_debug\DLLs\
    • xcopy /I C:\cpython\PCBuild\*.dll \Python27_debug\DLLs\
    • xcopy C:\cpython\PCbuild\python27_d.dll \Python27_debug\.
  • 64-bit
    • xcopy C:\cpython\PCBuild\\amd64\python_d.exe \Python27_debug\.
    • xcopy /I C:\cpython\PCbuild\amd64\*.pyd \Python27_debug\DLLs\
    • xcopy /I C:\cpython\PCBuild\amd64\*.dll \Python27_debug\DLLs\
    • xcopy C:\cpython\PCbuild\amd64\python27_d.dll \Python27_debug\.
  • xcopy /V /S /E /H /I C:\cpython\Lib \Python27_debug\Lib\.

 

You will also need to set your PYTHONPATH variable. Might as well update your PATH as well while you're at it.

  • set PYTHONPATH=\python27_debug\Lib:\python_27_debug\DLLs
  • set PATH=C:\python27_debug;C:\python27_debug\Scripts;%PATH%

 

Setuptools

You will need setup tools to build packages. The install python script can be found here: https://bootstrap.pypa.io/ez_setup.py Just run it to install.

 

Pip

Now you can go ahead and install pip with:

 

  • easy_install pip

 

VirtualEnv

Install virtualenv through pip via:

 

  • pip install virtualenv

 

Setup your VirtualEnv

Create your virtual environment now with your debug build of python using

 

  • virtualenv --python=python_d.exe debugPython
  • debugPython\Scripts\activate

 

Done!

If you've made it this far, you should have a working debug version of python running in a virtual environment. Remember, you should be sure to recompile your packages wherever possible. The major culprit will likely be those places with wheel files that have build-in dependencies for linking to msvcr90.dll.

 

Troubleshooting

error MSB8007 when building project

In my experience, this error has been due to the strange bug where the x64 compiler from Windows7.1SDK doesn't get installed or gets uninstalled. If you are getting this error, you can verify by the following: