What is the ctypes CDLL default path?

Viewed 3191

I'm trying to work with a library that is compiled to /usr/local/lib/libName.so but while running the python script that needs this file for:

from ctypes import CDLL
[...]
__lib = CDLL('libName.so')

I get:

OSError: libName.so: cannot open shared object file: No such file or directory

So i would like to know where i need to copy the .so file so that this CDLL call works properly.

1 Answers

ctypes ([Python 3.5]: ctypes - A foreign function library for Python), uses dlopen ([man7]: DLOPEN(3)) in order to load libraries, which delegates the loading (and implicitly finding) task to the Lnx loader.

The paths to search for .sos is very well explained in [man7]: LD.SO(8). Here's what it states about default ones:

  • In the default path /lib, and then /usr/lib. (On some 64-bit architectures, the default paths for 64-bit shared objects are /lib64, and then /usr/lib64.)

Ways (most common) to solve your problem:

  1. Pass the full path to CDLL:
    • __lib = CDLL("/usr/local/lib/libName.so")
  2. Tell the loader to also search /usr/local/lib for .sos, by adding it to ${LD_LIBRARY_PATH} env var for the python process that wants to load it:
    • export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib
    • LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib python
  3. Copy the .so in one of the default search paths (although I wouldn't recommend it, but if you must, copy it in /usr/lib instead of /lib)
Related