473,385 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Importing binary modules

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?

Mar 1 '07 #1
3 3765
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

an*************@gmail.com wrote:
cut ......................
>
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 - th*****@branda.to th********@gmail.com
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

iD8DBQFF5rcp1LDUVnWfY8gRAmLcAKCobvo06x84L0pj66amTB spTJ9nUwCg5sA+
MP7tLF/i8zqoZHl5Fxw2YsQ=
=BNQy
-----END PGP SIGNATURE-----

Mar 1 '07 #2
On 1 mar, 12:21, Thinker <thin...@branda.towrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

andre.dos.an...@gmail.com wrote:

cut ......................
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 - thin...@branda.to thinker...@gmail.comhttp://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

iD8DBQFF5rcp1LDUVnWfY8gRAmLcAKCobvo06x84L0pj66amTB spTJ9nUwCg5sA+
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?

Mar 1 '07 #3
-----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 - th*****@branda.to th********@gmail.com
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

iD8DBQFF5uWt1LDUVnWfY8gRAs52AKDLkGkNaPo2NgjdjFelNO x5KWG0KQCfT1RN
l6WqAWcutwPtmJPleSdkrr4=
=Ov/a
-----END PGP SIGNATURE-----

Mar 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vsevolod (Simon) Ilyushchenko | last post by:
Hi, Last year I have written a Perl module to serve as a backend to Macromedia Flash applications: http://www.simonf.com/amfperl I have just rewritten it in Python, which I have not used...
12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
0
by: Brandon Metcalf | last post by:
I come from a Perl and C background and have been given an application written in Python to maintain and I know very little about Python. I'm having trouble at run time with importing modules. ...
6
by: Kamilche | last post by:
I have a large project that is getting complex, and I would like to print the docstrings without importing the modules. The only Python utility I could find references is apparently defunct and...
6
by: Walter Brunswick | last post by:
What is the best way to import all modules in a directory (and possibly a subdirectory/subdirectories), possibly including conditionals, such as regexes?
0
by: Jure Vrscaj | last post by:
Hi, as title implies, I wrote a simple module that allows importing modules or even packages via http, conceptually similar to zipimport (of which I learned about during the process of writing...
14
by: T. Crane | last post by:
Hi, When troubleshooting code that's saved in a text file, I often find that I want to make a change to it, re-save it, then reimport it. However, just typing import myTestCode doesn't...
0
by: Martin P. Hellwig | last post by:
Hello all, I had some troubles in the past how to arrange my packages and modules, because I usually don't develop my stuff in the Lib\site-packages directory I have some troubles when importing...
7
by: Hussein B | last post by:
Hey, Suppose I have a Python application consists of many modules (lets say it is a Django application). If all the modules files are importing sys module, how many times the sys module will be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.