I’m trying to include Matlab shared library in python, if first I’m adding the following variables to the environment variable than the python code works fine:
$ export LD_LIBRARY_PATH=/usr/local/MATLAB/R2010b/runtime/glnxa64:/usr/local/MATLAB/R2010b/sys/os/glnxa64:/usr/local/MATLAB/R2010b/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2010b/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/R2010b/sys/java/jre/glnxa64/jre/lib/amd64
$ python run.py
The content of the run.py
from ctypes import *
if __name__ == '__main__':
dll = CDLL("/home/robu/Documents/tmo_compile/libmatrix/distrib/libmatrix.so")
I tried to add this environment variables in pythyon
from ctypes import *
import os
LD_LIBRARY_PATH = "/usr/local/MATLAB/R2010b/runtime/glnxa64:/usr/local/MATLAB/R2010b/sys/os/glnxa64:/usr/local/MATLAB/R2010b/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2010b/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/R2010b/sys/java/jre/glnxa64/jre/lib/amd64"
XAPPLRESDIR = "/usr/local/MATLAB/R2010b/X11/app-defaults"
if __name__ == '__main__':
os.environ['LD_LIBRARY_PATH'] = LD_LIBRARY_PATH
os.environ['XAPPLRESDIR'] = XAPPLRESDIR
print os.environ['LD_LIBRARY_PATH']
dll = CDLL("/home/robu/Documents/tmo_compile/libmatrix/distrib/libmatrix.so")
But in this case I’m getting and error
OSError: libmwmclmcrrt.so: cannot open shared object file: No such file or directory
What am I doing wrong in python?
UPDATED
I have tried the following:
if(not os.environ.has_key('LD_LIBRARY_PATH')):
os.environ['LD_LIBRARY_PATH'] = LD_LIBRARY_PATH
os.environ['XAPPLRESDIR'] = XAPPLRESDIR
os.execve('run.py', (), os.environ)
but I’m getting a OSError: [Errno 2] No such file or directory
That environment variable is read by the loader when it’s first loaded. It’s too late to set it in Python, since the loader was loaded when the Python executable started. You’ll need to use
os.exec*()to replace the current process via running a new executable (and hence loading a new loader).