0

here is my python package's __init__.py file

# mypackage/__init__.py
import ctypes

def init_lib():
    print("init_lib")
    lib = ctypes.CDLL("libcilin_similarity.dylib")

    lib.read_cilin('cilin.txt'.encode('UTF-8'))
    lib.similarity.restype = ctypes.c_float

    return lib


def compare(lib, first_word, second_word):
    print("this should work")
    print(second_word, lib.similarity(first_word.encode('UTF-8'), second_word.encode('UTF-8')))

If I'm running the __init__.py directly, it works pretty fine.

But if I'm trying to import the package from outside, it keeps giving me the OSError error:

OSError: dlopen(libcilin_similarity.dylib, 6): image not found

Could anyone help me prevent this error from happening? Many thanks!

the directory structure is

cilin_similarity
  |- __init__.py
  |- libcilin_similarity.dylib
try.py   # <- where I tried to import the package, but failed 

And when I try using absolute path, it gave me Error opening file error...

bolerovt
  • 623
  • 7
  • 18
  • When it fails, are you running it in a different directory, and/or with a different load path? – tripleee Jan 18 '18 at 12:04
  • Maybe look at https://stackoverflow.com/questions/856116/changing-ld-library-path-at-runtime-for-ctypes – tripleee Jan 18 '18 at 12:05
  • @tripleee thanks for replying. I just updated the directory structure, and as in the structure, the loading path is the same. – bolerovt Jan 18 '18 at 12:37
  • So you have no particular configuration to make it look for `"cilin_similarity/libcilin_similarity.dylib"` and it's fairly obvious then why it's failing, right? The other question has a number of solutions for how to configure `LD_LOAD_PATH` and related things. (I *think* an absolute path should work, though -- what exactly is the command which is failing? Some people say "absolute path" but then reveal that they used something which isn't an absolute path.) – tripleee Jan 18 '18 at 12:44
  • @tripleee that's right. When i was using absolute path, it works, please check my answer below. If you have a better way, please do let me know. Many many thanks! – bolerovt Jan 18 '18 at 12:49
  • For the record, if you are on a Mac (as apparently you are) the environment variable you want is `DYLD_LIBRARY_PATH` instead of `LD_LOAD_PATH`. There is an issue with SIP with that, too. Not really where I can compose a proper answer ... looks like there are a lot of unanswered near-duplicates. – tripleee Jan 18 '18 at 13:02

1 Answers1

0

When I am using absolute path, it works. And it turned out the Error opening file error was caused by other reason.

My absolute path code is:

path = os.path.dirname(os.path.realpath(__file__))
path_lib = path + "/libcilin_similarity.dylib"
lib = ctypes.CDLL(path_lib)

I'm not sure if it's the best solution. If you have a better answer please leave a post!

bolerovt
  • 623
  • 7
  • 18