Importing binary modules

  • Thread starter andre.dos.anjos
  • Start date
A

andre.dos.anjos

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?
 
T

Thinker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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.
Since you create a new module, you should try to link your module with
- -rpath-link option.
You can specify a path searched by linker when finding shared object
required.



- --
Thinker Li - (e-mail address removed) (e-mail address removed)
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF5rcp1LDUVnWfY8gRAmLcAKCobvo06x84L0pj66amTBspTJ9nUwCg5sA+
MP7tLF/i8zqoZHl5Fxw2YsQ=
=BNQy
-----END PGP SIGNATURE-----
 
?

=?iso-8859-1?B?QW5kcuk=?=

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) wrote:

cut ......................



Since you create a new module, you should try to link your module with
- -rpath-link option.
You can specify a path searched by linker when finding shared object
required.

- --
Thinker Li - (e-mail address removed) (e-mail address removed)://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org

iD8DBQFF5rcp1LDUVnWfY8gRAmLcAKCobvo06x84L0pj66amTBspTJ9nUwCg5sA+
MP7tLF/i8zqoZHl5Fxw2YsQ=
=BNQy
-----END PGP SIGNATURE-----

Well, unfortunately I don't have access to the binary like that. It is
given me, I just wanted to use it the way it was compiled. What could
be done then?
 
T

Thinker

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Well, unfortunately I don't have access to the binary like that. It
is given me, I just wanted to use it the way it was compiled. What
could be done then?
You have better set LD_LIBRARY_PATH environment variable, or use
ldconfig (plz, man ldconfig)
to add the directory, where you shared object is in, to the search
path of dynamic linker.

- --
Thinker Li - (e-mail address removed) (e-mail address removed)
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF5uWt1LDUVnWfY8gRAs52AKDLkGkNaPo2NgjdjFelNOx5KWG0KQCfT1RN
l6WqAWcutwPtmJPleSdkrr4=
=Ov/a
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top