Showing posts with label swig. Show all posts
Showing posts with label swig. Show all posts

Wednesday, May 28, 2008

Writing a Python Extension with SWIG, GCC on Cygwin

UPDATE : Please see comments below this post - the latest version of SWIG appears to have made the "setup.py" step described below - redundant.

t seemed easy at first. Many books document it, several folks blog about it, articles have been written about it, but I didn't quite seem to get it right. Here are my steps and missteps in getting the python sample from the SWIG tutorial to work right on my machine with cygwin 1.5.25, gcc 3.4.4, swig 1.3.35, python 2.5 already installed.

1. Download the sample example.c from the SWIG Tutorial.

2. Get the interface file from the same location and save a local copy in your working directory as say sample.i.

3. Run the swig compiler on it using 'swig -python sample.i' to get the machine generated python extension - example_wrap.c

If you are past this step, you have swig properly installed on your computer. We will now use the python distools package to do the rest of the magic for us.

4. Create a setup.py file that contains all dependencies required to create the Python extension. This is the critical step ! Here is what mine looks like .

from distutils.core import setup, Extension

module1 = Extension('_example', sources=['example_wrap.c',
'example.c'
])
setup (name = 'example',
version = '1.0',
description = 'Simple example from SWIG tutorial',
ext_modules = [module1])

Note the underscore (_) before the module name. This little mistake could trip you up completely and lead to errors from the build process such as :

"Cannot export initexample: symbol not defined"

Apparently SWIG requires that you use an "-" before your module name. Also note that the sources attribute lists the C program and the swig-generated C program. It is also possible to list only the swig interface file as in sources= 'example.i' and allow python to complete Step 3 from above. This did not work for me. It trips up the linking process at a later point. Why ? - I have no clue.


5. Run the Python distool to generate the extension . Note the use of "inplace". This is convenient when developing as it generates the .pyd file (Python extension) in the current working directory. You may remove this if you want it in a standard "build" directory.

python setup.py build_ext --compiler=mingw32 --inplace


NOTE: The '--compiler=mingw32' option is to force compilation using the mingw32 compiler. This can be automated as described here . The mingw compiler should have been installed if you kept the defaults while installing GCC on cygwin. You may also install it separately from here or running the cygwin installer again and choose the mingw option along with its dependencies.

If all goes well, you should see no errors and upon completion, find a file named '_example.pyd' in your working directory.

6. Install the extension by running "python setup.py install" . Alternately simply copy the _example.pyd file to your Python install (e.g. c:\Python25\Lib\site_packages) .

7. If you fire up IDLE and run '>> import example' , you'll probably see an import error . However if you type "import _example", it'll succeed. This is how SWIG generates your module. You probably don't want to use your module this way. The way to fix it is to look for a python file named "example.py" in your current working directory. Copy this file to the same location as above (e.g. c:\Python25\Lib\site_packages) .

8. >>> import example as e

9. >>> e.fact(5)
120




References :


Here are several links I read up to get this example working - my thanks to all those authors. I'll caution though that a lot of the hacks about using pexports etc. that you'll find on many of these web sites are unnecessary if you are using Python 2.5. The Python 2.5 distribution comes with the import library. On my machine this is C:\Python25\libs\libpython25.a.