Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python loading c lib with CDLL, doesn't see libraries in the python path

I'm trying to get some open source academic code working (the project home is here). It is a big C++ codebase with a (very) thin python wrapper which uses CDLL to load the C++ and call some C functions that are available to allow primitive python scripting of the code.

However, the initial import code crashes because it can't find the .so files sitting next to it in site-packages:

in the installed file:

from ctypes import *

try:
  self.lib = CDLL("_lammps.so")
except:
  try:
    self.lib = CDLL("_lammps_serial.so")
  except:
    raise OSError,"Could not load LAMMPS dynamic library"

and in a script or the interpreter:

from lammps import lammps
l = lammps()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lammps.py", line 42, in __init__
    raise OSError,"Could not load LAMMPS dynamic library"
OSError: Could not load LAMMPS dynamic library

Other answers might seem to have this covered, but this only works if CDLL() is called within the script actually invoked (or the working directory of the prompt that ran the interpreter) - i.e. if the 'relative path' is in user-space, rather than python-library-space.

How do we reliably install for import a C/C++ library that we built ourselves? Short of polluting the system library locations like /usr/lib, which isn't very pythonic, I can't see an easy solution.

(EDIT: corrected function names, unclear refactoring unhelpful! sorry!)

like image 827
tehwalrus Avatar asked Feb 04 '12 18:02

tehwalrus


People also ask

What is CDLL in Python?

cdll loads libraries which export functions using the standard cdecl calling convention, while windll libraries call functions using the stdcall calling convention. oledll also uses the stdcall calling convention, and assumes the functions return a Windows HRESULT error code.

How does Python find shared libraries?

We can see that the system python only looks for its shared libraries in the ~ directory, some specified in the PYTHONPATH and some directories in /usr/lib and /usr/local/lib (mainly /usr/lib ), and only looks for the dist-packages instead of site-packages.

What is C_char_p?

c_char_p is a subclass of _SimpleCData , with _type_ == 'z' . The __init__ method calls the type's setfunc , which for simple type 'z' is z_set . In Python 2, the z_set function (2.7.

What is C_ubyte?

The cubit, generally taken as equal to 18 inches (457 mm), was based on the length of the arm from the elbow to the tip of the middle finger and was considered the equivalent of 6 palms or 2 spans.


2 Answers

Im on linux, all I did to fix this issue was put in the absolute path from the os module, and it works

from ctypes import *
import os

xss = cdll.LoadLibrary(os.path.abspath("libxss.so.1"))
print xss.xss_test_1()

This is python 2.7 as well.

like image 152
Josh Weinstein Avatar answered Oct 16 '22 00:10

Josh Weinstein


You must specify the absolute path. Try the following:

import os
from ctypes import *

try:
  slib = os.getcwd() + '/' +'_lammps.so'
  hlibc = CDLL(slib)
  hilbc.FunctionName()
except:
  try:
    slib = os.getcwd() + '/' +'_lammps_serial.so'
    hlibc = CDLL(slib)
  except:
    raise OSError,"Could not load LAMMPS dynamic library"
like image 37
Adarsh Maurya Avatar answered Oct 15 '22 23:10

Adarsh Maurya