Change current process environment’s LD_LIBRARY_PATH

The reason

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

doesn’t work is simple: this environment variable controls behavior of the dynamic loader ( on Linux, 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 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/ via its absolute path before importing When you then import, the loader will discover that it has already loaded, and will use the already loaded module instead of searching for it again.

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

Leave a Comment