Loading python c lib from CDLL, does not see libraries in python path

I am trying to run an open source tutorial (project home here ). This is a large C ++ code base with a (very) thin python shell that uses CDLLC ++ to load and calls some C functions that are available for primitive code writing in python code.

However, the import source code fails because it cannot find the .so files sitting next to it in the 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 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

, , , CDLL() script ( , ), , " " , python-library.

C/++, ? , /usr/lib, pythonic, .

(EDIT: , ! !)

+5
3

Im on linux, , , , os,

from ctypes import *
import os

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

python 2.7.

+2

strace -eopen, - :

open("tls/x86_64/_lammps.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("tls/_lammps.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("x86_64/_lammps.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("_lammps.so", O_RDONLY|O_CLOEXEC)  = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 6
open("/lib/_lammps.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/usr/lib/_lammps.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)

, python ctypes . , , , .

+1

__file__ , . os.path , __file__, . - :

temp = os.path.abspath(__file__)
temp = os.path.realpath(temp)
temp = os.path.dirname(temp)
temp = os.path.join(temp, "_lammps.so")
lib = CDLL(path)

(.. .dll .dylib .so, lib , , ) , . , glob.glob, .

, , . ctypes.util.find_library ( ) (, , ). , PYTHONPATH , ( ).

And again it seems that if you just added the correct directory to LD_LIBRARY_PATH, you should be able to download it .

+1
source

All Articles