Importing binary modules

andre.dos.anjos at andre.dos.anjos at
Thu Mar 1 11:51:19 CET 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 It resides
in '/my/module/dir'. Internally, it loads, which
implements most of the functionality for MyNewModule, so the user can

import sys
import MyNewModule

And everything works fine.

The problem appears when depends on another library,
say, 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 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
#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