building extensions for Windows Python

Michael L Torrie torriem at chem.byu.edu
Fri Oct 13 19:41:05 EDT 2006


On Fri, 2006-10-13 at 11:39 -0400, JW wrote:
> I have a lousy little Python extension, generated with the generous help
> of Pyrex.  In Linux, things are simple.  I compile the extension, link it
> against some C stuff, and *poof*! everything works.
> 
> My employer wants me to create a Windows version of my extension that
> works with the vanilla Python 2.5 from python.org.
> 
> My employment contract states that I won't be required to run Windows, and
> I desperately want to honor that clause. Ideally,  I'd somehow MinGw cross
> compile like I do with C/C++ and *poof*!, out would pop a file I could
> hand out to someone who wanted to "import <my_lousy_extension>".

I took a very simple pyrex file that compiled and worked fine on linux
and built it for windows using my mingw cross compiler.  

First I borrowed a complete windows python installation and stuck it on
my linux drive (in my case in /usr/i386-pc-mingw32/Python2.4.

Then I just did:

$ pyrex test.pyx

$ i386-pc-mingw32-gcc -o test.dll test.c -I/usr/i386-pc-
mingw32/include/python2.4 -mwindows -shared -lpython24 -L/usr/i386-pc-
mingw32/Python2.4/lib

Since there were no special windows libraries used, there were no code
changes required at all.  So look into the mingw cross compiler.
Usually you have to build it yourself.  see
http://www.torriefamily.org/~torriem/cross for more information.  The
information on the windows target is in the second half of the document.
This document used the OS X as the build host, but the same steps work
on linux.  I prefer to use /usr/local or /usr as my prefix (ending up
with /usr/local/bin/i386-pc-mingw32-gcc etc)

Michael


> 
> In Windows, things aren't so simple.  I'm immediately bedazzled by a vast,
> unfamiliar nomenclature: "VC6", "VC7.1", "msvcrt", "msvcr71", "msvcr80",
> "VS2005", "VS2007", "MSVC2003", "MSVC2005", "Express Edition", ".NET SDK",
> etc. 
> 
> The python.org "recommended compiler" is apparently no longer
> available, but someone posted a link to an obscure Microsoft webpage
> artifact where it could still be obtained.  Running it under WINE is less
> than ideal, but I decided to try and make it work.  However, when I tried
> installing the compiler, it failed because my "Internet Explorer [was] out
> of date."  I'm not sure how IE comes into the picture, but I'm just a
> programmer, after all.
> 
> I am further confused by the python.org docs on using MinGw to build
> extensions:  "These instructions only apply if you're using a version of
> Python prior to 2.4.1 with a MinGW prior to 3.0.0", but further on:
> "pcexports python25.dll >python25.def" seems to contradict that.  It
> really makes no difference.  The referenced webpage where the "pcexports"
> tool is found seems defunct.
> 
> My main problem is that I don't really grasp the big picture.  Can someone
> give me an overview of the requirements to build extensions for Windows
> Python, circa 2.5?  Or, can I tell my employer she'll just have to
> hire/contract a Windows expert to do the mud wrestling?
> 
> Jim Wilson
> Gainesville, FL




More information about the Python-list mailing list