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.

6 comments:

Unknown said...

I tried this using MinGW & Python 2.6 but not Cygwin. Due to a known issue with MinGW I had to remove the get_time() function. The other two functions then worked. Thanks for the article.

Russ said...

I am amazed! You are a true genius!

I had to make a few modifications for my cygwin system using gcc:

gcc -c example.c example_wrap.c -I/usr/include/python2.6/

gcc -shared example.o example_wrap.o -lpython2.6 -o _example.so

I used your script "setup.py". Note that there is a typo: you are missing "[" right after "sources=".

After running setup.py, the import worked fine.

Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
[GCC 4.3.4 20090804 (release) 1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> example.fact(12)
479001600
>>>

Crazy how the trivial little tutorial example is not so trivial.

Thanks for your post. Now my life can continue.

Raghavan said...

@ Dr.Bowers : thanks ! wow, i am amazed how an ancient post on an inactive blog can suddenly become useful.

Russ said...

p.s. The swig provided on the cygwin site is 1.3.40.

Thinking that the problem could be a bug in the old version of swig, I downloaded, compiled and installed the most recent version of swig (2.0.4), but I got the same error messages when trying to import, following the swig tutorial example.

Your setup.py script is still needed.

Do you know who we needs to be informed about this bug? This is kind of ridiculous.

Thanks again!

Russ said...

Please disregard my post. Swig 2.0.4 works as described with the tutorial example. I guess they fixed the bug.

The module _example.so is generated correctly and imports correctly. It was not necessary to use the setup.py script.

Kudos to the swig developers!

Raghavan said...

@ Dr.Bowers : good to know !