Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing LD_LIBRARY_PATH at runtime for ctypes

Tags:

python

ctypes

How do you update this environment variable at runtime so that ctypes can load a library wherever? I've tried the following and neither seem to work.

from ctypes import * os.environ['LD_LIBRARY_PATH'] = "/home/starlon/Projects/pyCFA635/lib"   os.putenv('LD_LIBRARY_PATH', "/home/starlon/Projects/pyCFA635/lib")   lib = CDLL("libevaluator.so") 
like image 916
Scott Avatar asked May 13 '09 04:05

Scott


People also ask

What should be in LD_LIBRARY_PATH?

Set the LD_LIBRARY_PATH to include the directory or directories that contain your libraries.

What is Ctypes Byref?

ctypes exports the byref() function which is used to pass parameters by reference.

What does Ctypes do in Python?

ctypes allows to create C callable function pointers from Python callables. These are sometimes called callback functions. First, you must create a class for the callback function, the class knows the calling convention, the return type, and the number and types of arguments this function will receive.


1 Answers

By the time a program such as Python is running, the dynamic loader (ld.so.1 or something similar) has already read LD_LIBRARY_PATH and won't notice any changes thereafter. So, unless the Python software itself evaluates LD_LIBRARY_PATH and uses it to build the possible path name of the library for dlopen() or an equivalent function to use, setting the variable in the script will have no effect.

Given that you say it doesn't work, it seems plausible to suppose that Python does not build and try all the possible library names; it probably relies on LD_LIBRARY_PATH alone.

like image 153
Jonathan Leffler Avatar answered Sep 21 '22 14:09

Jonathan Leffler