Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change current process environment's LD_LIBRARY_PATH

Is it possible to change environment variables of current process?

More specifically in a python script I want to change LD_LIBRARY_PATH so that on import of a module 'x' which depends on some xyz.so, xyz.so is taken from my given path in LD_LIBRARY_PATH

is there any other way to dynamically change path from where library is loaded?

Edit: I think I need to mention that I have already tried thing like os.environ["LD_LIBRARY_PATH"] = mypath os.putenv('LD_LIBRARY_PATH', mypath)

but these modify the env. for spawned sub-process, not the current process, and module loading doesn't consider the new LD_LIBRARY_PATH

Edit2, so question is can we change environment or something so the library loader sees it and loads from there?

like image 960
Anurag Uniyal Avatar asked Jul 24 '09 14:07

Anurag Uniyal


People also ask

What does LD_LIBRARY_PATH mean?

LD_LIBRARY_PATH is the default library path which is accessed to check for available dynamic and shared libraries. It is specific to linux distributions. It is similar to environment variable PATH in windows that linker checks for possible implementations during linking time.

How do I change the library path in Linux?

How do I set the Library path under Linux operating systems? You need to use ldconfig config file and ldconfig command which creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld. so.

What is the difference between path and LD_LIBRARY_PATH?

PATH is for specifying directories of executable programs. LD_LIBRARY_PATH is used to specify directories of libraries. From other point of view, PATH is used primarily by the shell, while LD_LIBRARY_PATH is used by the dynamic loader (usually ld-linux.so ).

Why LD_LIBRARY_PATH is not inherited?

When running external program from stored procedure, there will be no LIBPATH or LD_LIBRARY_PATH environment variable for external program, if the external program is depending on the dynamic libraries outside the system default library path, it may failed loading dynamic libraries.


2 Answers

The reason

os.environ["LD_LIBRARY_PATH"] = ... 

doesn't work is simple: this environment variable controls behavior of the dynamic loader (ld-linux.so.2 on Linux, ld.so.1 on Solaris), but the loader only looks at LD_LIBRARY_PATH once at process startup. Changing the value of LD_LIBRARY_PATH in the current process after that point has no effect (just as the answer to this question says).

You do have some options:

A. If you know that you are going to need xyz.so from /some/path, and control the execution of python script from the start, then simply set LD_LIBRARY_PATH to your liking (after checking that it is not already so set), and re-execute yourself. This is what Java does.

B. You can import /some/path/xyz.so via its absolute path before importing x.so. When you then import x.so, the loader will discover that it has already loaded xyz.so, and will use the already loaded module instead of searching for it again.

C. If you build x.so yourself, you can add -Wl,-rpath=/some/path to its link line, and then importing x.so will cause the loader to look for dependent modules in /some/path.

like image 98
Employed Russian Avatar answered Sep 18 '22 10:09

Employed Russian


Based on the answer from Employed Russian, this is what works for me

oracle_libs = os.environ['ORACLE_HOME']+"/lib/" rerun = True  if not 'LD_LIBRARY_PATH' in os.environ:   os.environ['LD_LIBRARY_PATH'] = ":"+oracle_libs elif not oracle_libs in os.environ.get('LD_LIBRARY_PATH'):   os.environ['LD_LIBRARY_PATH'] += ":"+oracle_libs else:   rerun = False  if rerun:   os.execve(os.path.realpath(__file__), sys.argv, os.environ) 
like image 42
cristi Avatar answered Sep 20 '22 10:09

cristi