Importing binary modules

andre.dos.anjos at gmail.com andre.dos.anjos at gmail.com
Thu Mar 1 05:51:19 EST 2007


How can I, in python (linux, python version >= 2.4.2), dynamically set
the path and import a binary module that depends on libraries which
are not declared in LD_LIBRARY_PATH or any other automated linker path
when python starts?

This is a an example:

Suppose I have a new python module, called MyNewModule.py. It resides
in '/my/module/dir'. Internally, it loads libMyNewModule.so, which
implements most of the functionality for MyNewModule, so the user can
say:

import sys
sys.path.append('/my/module/dir')
import MyNewModule

And everything works fine.

The problem appears when libMyNewModule.so depends on another library,
say libfoo.so, which sits also in /my/module/dir. In that case, '/my/
module/dir', needs to be preset in the LD_LIBRARY_PATH *before* the
python interpreter is set.

In this case, the snippet of code above will not work as one would
expect. It will normally give an ImportError saying it cannot find
libfoo.so. That happens, apparently, because the dynamic linker cannot
rescan LD_LIBRARY_PATH after the python interpreter has started.

So is there any other way to circumvent this?

import sys
sys.path.append('/my/module/dir')
#black magic to get the linker to reload the LD_LIBRARY_PATH
import MyNewModule

So this succeeds?




More information about the Python-list mailing list