Compile Python 2.5 Packages with MinGW

Friday, March 18, 2011

In Python 2.5, default compiler is Visual Studio 2003. If you don't have the complier, you may end up with the following error message:

error: Python was built with Visual Studio 2003;
extensions must be built with a compiler than can generate compatible binaries. Visual Studio 2003 was not found on this system. If you have Cygwin installed, you can try compiling with MingW32, by passing "-c mingw32" to setup.py.

I don't have Visual Studio 2003 and therefore I try to follow the msg to download MingW32. Here are the steps:

1. Download MinGW from http://sourceforge.net/projects/mingw/ and install. It seems we better install the complier in default folder location c:\MinGW.

2. During the installation, I selected "C++ Compiler", "MSYS Basic System" and "MinGW Developer ToolKit".



3. Update your PATH: Add the C:\MinGW\bin directory to the system PATH. You may need to log-off and log-in again to reflect the added path.

4. Go to C:\Python25\Lib\distutils, create a file distutils.cfg and add the following lines:
[build]
compiler=mingw32

5. If you compile the package at this stage with "Python setup.py install", you may get the following error:

File "C:\Python25\lib\distutils\cygwinccompiler.py", line 84, in __init__
get_versions()
File "C:\Python25\lib\distutils\cygwinccompiler.py", line 424, in get_versions

ld_version = StrictVersion(result.group(1))
File "C:\Python25\lib\distutils\version.py", line 40, in __init__
self.parse(vstring)
File "C:\Python25\lib\distutils\version.py", line 107, in parse
raise ValueError, "invalid version number '%s'" % vstring
ValueError: invalid version number '2.20.51.20100613'

6. It says that your compiler version is invalid. Go to C:\Python25\Lib\distutils and backup a copy of cygwinccompiler.py. Open cygwinccompiler.py in text editor (e.g. notepad or IDLE). In def get_versions(): section you will find the following line:

result = re.search('(\d+\.\d+(\.\d+)*)',out_string)

Replace all "result = re.search('(\d+\.\d+(\.\d+)*)',out_string)" with the following code:

result = re.search('(\d+(\.\d+)?(\.\d+)?)',out_string)

It will tell Python to check the first digit only.

7. Try to compile again and it should work now.

8. Since I am working with Python 2.5.2, I am not sure 2.6 or 2.7 have that kind of issue or not.

Please tell me if it works for you are not.

Reference:
http://bugs.python.org/issue2234