0

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 ?

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

6 Comments

I made sure liba.so and libb.so are both in my path . Still have the same problem. libb.so ( 3rd party library)not found
It is able to find liba.so .. but complains about libb.so which is linked with liba.so. I made sure that libb.so is also in the path
So if you do CDLL(find_library('b')) it works? Also noticed that you're importing liba slightly strangely, you should either do import liba or from lib import * not import liba * the latter is invalid syntax `
The 3rd party function calls are too complex and my api is designed to shield the python user from that.
Also I tracked the problem to the dlopen where it tries to resolve all the symbols. I want the dlopen to be in the RTLD_LAZY mode. I guess I can do that only via ctypes(lib_name. mode=1). Is there a way to do it via the import statement ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.