Compiling disutil modules on Windows

Geoff Caplan geoff at variosoft.com
Sun Aug 8 11:18:42 CEST 2004


Hi folks,

I have Sébastien Sauvage's example extension compiled and working.

For the record, here is what I did, updated for Python 2.3.

For the gory details, see:

http://sebsauvage.net/python/mingw.html

1) Install MinGW

- Go to http://www.mingw.org/download.shtml

- Find the current version of MinGW. At the time of writing, this is
MinGW-3.1.0-1.exe. It's around 14 megs.

- Run the install

- Rename the MinGW directory to gcc, if desired


2) Install SWIG

- Go to http://sourceforge.net/project/showfiles.php?group_id=1645

You are looking for SWIGWIN.

- rename the installed directory to Swig, if desired


3) Add gcc and Swig to your Autoexec.bat file

This is normally found at C:\Autoexec.bat

Mine looked like this:

SET PATH=c:\gcc\bin;%PATH%
SET PATH=c:\Swig;%PATH%

I rebooted to activate these paths. If you knew Windoze better than
I do, there is probably a more elegant way to do this.


4) Copy python23.dll to your Python libs directory

Search C:\ for python23.dll.

On Win2000, you will find it in c:\WINNT\system32\

Copy it to C:\Python23\libs\


5) Compile a test extension

------------------------------------------
- Create the setup.py file (note the underscore on the extension name:
this seems to be a requirement)

# setup.py
import distutils
from distutils.core import setup, Extension
setup(name = "Simple example from the SWIG website",
        version = "2.3",
        ext_modules = [Extension("_example", ["example.i","example.c"])])

------------------------------------------
- Create the example.c file

/* File : example.c */
#include <time.h>
double My_variable = 3.0;

int fact(int n) {    
        if (n <= 1) return 1;    
        else return n*fact(n-1);    
        }

int my_mod(int x, int y) {    
        return (x%y);    
        }
        
char *get_time() {    
        time_t ltime;    
        time(&ltime);    
        return ctime(&ltime);    

------------------------------------------
- Create the example.i file

/* example.i */
%module example
%{
/* Put header files here (optional) */
%} 
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time()

------------------------------------------

- Now launch your dos shell and cd to the directory containing your
example files.

- Run the command: python setup.py build -cmingw32

If all goes well, this will create a directory "build" in the same
directory as your files. You will find your new dll "_example.pyd"
under lib.win32

- Copy the dll to E:\Python23\DLLs\


6) Run your test extension

- Launch a Python shell and run:

>>> import _example
>>> _example.fact(5)
120
>>> _example.get_time()
'Sun Aug 08 10:09:25 2004\n'

I tried renaming the extension without the leading underscore but this
generates an error. I am new to Python: perhaps a SWIG guru could
explain why the underscore is required, or if not required, how it can
be avoided?

Hope someone finds this useful...

------------------ 
Geoff Caplan
Vario Software Ltd
(+44) 121-515 1154 




More information about the Python-list mailing list