Thursday, May 19, 2011

Python Extension in C using existing code base

Whoosh !!

Yes as an avid C programmer always thinks, I was looking for an opportunity to write code in C and utilize the same in Python. I came across a nice documentation at the link "http://docs.python.org/extending/extending.html?highlight=dynamic%20linking".

I read the same but it was cakewalk till the time I was writing the code as mentioned.
My next target was to utilize our teams existing C code to build the Python Extension to be utilized for some scripting purpose.

Worst part came when I was trying to build the extension and dynamically loading the same in Python, it gave "ImportError".
I frantically started looking for some online documentation. No clue!!
Probably still very few people try developing it without different packages available .

Next my explorations started. And after a real hard time I found that the dynamic library file, i.e., the xxx.so I was generating was not getting linked probably leading to this error.

Linked it properly and whoozz! It works. :-)

So, the learning here is that the complete linking should be available in the library or else will lead to ImportError.