3

I am trying to load a .so library in Python using ctypes. This library has many files in /home/me/my_library. When I load

from ctypes import *
lib = CDLL('/home/me/my_library/main.so')

I get

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

Note that the error is with not_main.so and not with main.so. All the files are within /home/me/my_library. I think the problem is that I have to add the path to this library, which in Windows can be done using os.add_dll_directory. How would I do this in Linux? Is there a cross-platform way of doing this?

Note I have tried with os.environ['LD_LIBRARY_PATH'] = '/home/me/my_library/' but it does not work.

user171780
  • 2,243
  • 1
  • 20
  • 43

1 Answers1

0

As I understand until python 3.8 on both Linux and Windows to add search path you would need to add it to the PATH env variable:

if not os.environ['PATH'].__contains__('/path/to/dll_or_so'):
  os.environ['PATH'] = '/path/to/dll_or_so' + os.pathsep + os.environ['PATH']

Starting from python 3.8 this approach doesn't seem to work on Windows and new function was introduced to solve this task: os.add_dll_directory(path)

Thus in your case (Linux) you should modify os.environ['PATH']

See previous answer on similar topic

Kerim
  • 171
  • 3
  • 10