Reputation: 192
I'm trying to load the shared library which internally uses some resources. File structure is the following:
dir_a/script.py
dir_a/lib/lib.so
dir_a/model/model.bin
From script.py
I'm using the following code to load tge library:
from ctypes import *
from os import path
script_dir = path.dirname(path.realpath(__file__))
lib_path = path.join(script_dir, "lib/lib.so")
lib = cdll.LoadLibrary(lib_path )
This allows me to run the script not only from script directory, but from the other directories as well and library location is resolved properly:
cd dir_a/lib
python ../script.py
But I'm facing with model.bin
file access issue. Looks like library tries to load it using model/model.bin
string literal and it is resolved only when I'm running the script from dir_a
directory.
Is it possible to somehow specify working directory to loaded library to be the root directory to relative paths?
Upvotes: 1
Views: 147
Reputation: 41137
Most likely (I can't say for sure without looking at the code) lib.so expects the resource file to be present at model/model.bin. That's relative to current directory (CWD).
One way of overcoming this is setting it (before loading the .so), via [Python.Docs]: os.chdir(path):
os.chdir(script_dir)
Might also want to check:
[SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer)
[SO]: Independent CDLL Library Instances with Ctypes (@CristiFati's answer)
Upvotes: 0