Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the ctypes CDLL default path?

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.

like image 287
meissner_ Avatar asked Sep 19 '18 08:09

meissner_


People also ask

What is ctypes CDLL?

ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.

Does ctypes work with C++?

ctypes is the de facto standard library for interfacing with C/C++ from CPython, and it provides not only full access to the native C interface of most major operating systems (e.g., kernel32 on Windows, or libc on *nix), but also provides support for loading and interfacing with dynamic libraries, such as DLLs or ...

What is CDLL Msvcrt?

cdll. msvcrt loads msvcrt. dll , which is a library that ships as part of Windows. It is not the C runtime that Python links with, so you shouldn't call the malloc/free from msvcrt . For example, for Python 2.6/3.1, you should be using ctypes.


Video Answer


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)
like image 150
CristiFati Avatar answered Oct 05 '22 04:10

CristiFati