calling third party c functions from python
Question: I have a requirement of calling third party c functions from inside python. To
do this I created a c api which has all the python specific c code ( using
METH_VARARGS) to call the third party functions. I linked this code liba.so
with the 3 party library libb.so In my python file , I'm doing:

    
    
    import liba *
    

Python now complains libb.so not found. What am I doing wrong ?


Answer: You have to include `liba.so` in your PATH, otherwise Python won't know where
to look for it.

Try the following code, it'll load the library if it can find it from PATH,
otherwise it'll try loading it from the directory of the load script

    
    
    from ctypes import *
    from ctypes.util import find_library
    import os
    
    
    if find_library('a'):
        liba = CDLL(find_library('a'))
    else:
        # library is not in your path, try loading it from the current directory
        print 'liba not found in system path, trying to load it from the current directory'
        print '%s/%s'%(os.path.dirname(__file__),'liba.so')
        liba = CDLL(os.path.join(os.path.dirname(__file__),'liba.so'))
    

<http://docs.python.org/library/ctypes.html#finding-shared-libraries>

**UPDATE:** I was wondering why you created a native library (`liba`) to
access a native 3rd party library (`libb`). You can import the third party c
library straight into python using `ctypes` and create a python (not native)
wrapper for `libb`. For instance to call the standard c lib `time` you would
do

    
    
    >>> from ctypes import *
    >>> lib_c = CDLL("libc.so.6")
    >>> print lib_c.time(None)
    1150640792
    

and for libb

    
    
    >>> from ctypes import *
    >>> lib_b = CDLL("libb")
    >>> lib_b.hello_world(None)
    

