[Tutor] Can I set LD_LIBRARY_PATH within a .py file?

Albert-Jan Roskam fomcl at yahoo.com
Sun Oct 16 20:25:44 CEST 2011


Hi Hugo,
 
You are absolutely right. Thank you! It took me a lot of reading and tinkering to find out that typing the following in the terminal works:
export LD_LIBRARY_PATH=\path\to\the\lib
python main.py # contains import to my program + calls to the functions in it.
 
I find it strange though, that ctypes.CDLL() does not accept library names *with the full path*. In Linux, you could do it, but it seems that all the dependencies of the libary in that non-standard location are looked for ONLY in that non-standard location.

Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From: Hugo Arts <hugo.yoshi at gmail.com>
>To: Albert-Jan Roskam <fomcl at yahoo.com>
>Cc: Python Mailing List <tutor at python.org>
>Sent: Sunday, October 16, 2011 1:22 AM
>Subject: Re: [Tutor] Can I set LD_LIBRARY_PATH within a .py file?
>
>On Sat, Oct 15, 2011 at 9:51 PM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
>> Hello,
>> Can I set the LD_LIBRARY_PATH environment variable (on-the-fly) within a .py
>> file?
>> I would like to use an .so-file that lives in a non-standard location.
>>
>> This does not work:
>> try:
>>    os.environ["LD_LIBRARY_PATH"]  += (":" + path)
>> except KeyError:
>>    os.environ["LD_LIBRARY_PATH"] = path
>> Currently, I can only run the program in the terminal:
>> export LD_LIBRARY_PATH=/home/dude/Desktop/test
>> python /home/dude/Desktop/testLoadLibLinux.py
>> This works (yaaayy!), but I'd like to run the .py file directly.
>> Is this possible? I also don't  like the fact that I can't test the .py file
>> in Idle.
>> Perhaps a complicating factor is a bug in LD_LIBRARY_PATH
>> in Linux Ubuntu 10 (the version I'm using):
>> https://bugs.edge.launchpad.net/ubuntu/+source/xorg/+bug/366728
>> https://bugs.launchpad.net/ubuntu/+source/xorg/+bug/366728/comments/21
>> solution:
>> sudo gedit /etc/X11/Xsession.options
>> (change "use-ssh-agent" into "no-use-ssh-agent")
>>
>> Thank you in advance for your thoughts!
>>
>> Cheers!!
>> Albert-Jan
>>
>
>Alright, I'm not going to pretend to be an expert on this one, a bit
>of this is speculation and inference from what I know about dynamic
>linking. In short, I don't think you can modify LD_LIBRARY_PATH on the
>fly and have it actually work. The reason for this is that the linker
>runs and finds all the libraries *before* the python process actually
>starts. So by the time you go and modify the environment, all
>libraries have already been linked, and your modified variable is
>never even read by the linker.
>
>So the best you can do is write a tiny wrapper to set LD_LIBRARY_PATH
>and then run your actual script through it. Or you could set the
>environment variable and then fork(), I suppose, since the child will
>inherit the modified environment. But the wrapper is your simplest
>option.
>
>HTH,
>Hugo
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111016/792dc422/attachment.html>


More information about the Tutor mailing list