Python without wrapper script

Discussion in 'Python' started by eric.frederich, Dec 2, 2009.

  1. Is there a way to set up environment variables in python itself
    without having a wrapper script.

    The wrapper script is now something like....

    #!/bin/bash

    export LD_LIBRARY_PATH="/some/thing/lib:$LD_LIBRARY_PATH"
    export LD_LIBRARY_PATH="/another/thing/lib:$LD_LIBRARY_PATH"

    export PATH="/some/thing/bin:$PATH"
    export PATH="/another/thing/bin:$PATH"

    python ./someScript.py
     
    eric.frederich, Dec 2, 2009
    #1
    1. Advertising

  2. eric.frederich wrote:
    > Is there a way to set up environment variables in python itself
    > without having a wrapper script.


    Yes, sure, you can set environment variables...

    > The wrapper script is now something like....
    >
    > #!/bin/bash
    >
    > export LD_LIBRARY_PATH="/some/thing/lib:$LD_LIBRARY_PATH"
    > export LD_LIBRARY_PATH="/another/thing/lib:$LD_LIBRARY_PATH"
    >
    > export PATH="/some/thing/bin:$PATH"
    > export PATH="/another/thing/bin:$PATH"
    >
    > python ./someScript.py


    ....but this won't work, I'm afraid.

    LD_LIBRARY_PATH is for the program loader / dynamic linker under Linux. This
    thing is what is invoked _before_ the program is started, any later
    modifications to the environment are ignored.

    Similarly PATH, which tells the shell (e.g. bash) where to find executables.
    If you need that to e.g. find 'python' itself, you're out of luck.
    Otherwise, I believe Python itself doesn't use PATH, so you could set it
    inside and any shells started from Python should pick it up.

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
     
    Ulrich Eckhardt, Dec 2, 2009
    #2
    1. Advertising

  3. eric.frederich wrote:
    > Is there a way to set up environment variables in python itself
    > without having a wrapper script.
    >
    > The wrapper script is now something like....
    >
    > #!/bin/bash
    >
    > export LD_LIBRARY_PATH="/some/thing/lib:$LD_LIBRARY_PATH"
    > export LD_LIBRARY_PATH="/another/thing/lib:$LD_LIBRARY_PATH"
    >
    > export PATH="/some/thing/bin:$PATH"
    > export PATH="/another/thing/bin:$PATH"
    >
    > python ./someScript.py
    >

    try in someScript.py

    os.environ['PATH'] = "/some/thing/bin:"+ os.environ['PATH']

    example:

    import subprocess
    import os
    p = subprocess.Popen('/bin/echo $TEST', shell=True, stdout=subprocess.PIPE )
    p.communicate()[0]
    > '\n'


    os.environ['TEST'] = 'hello'
    p = subprocess.Popen('/bin/echo $TEST', shell=True, stdout=subprocess.PIPE )
    p.communicate()[0]
    > 'hello\n'


    JM
     
    Jean-Michel Pichavant, Dec 2, 2009
    #3
  4. eric.frederich

    Hans Mulder Guest

    Ulrich Eckhardt wrote:
    > eric.frederich wrote:
    >> Is there a way to set up environment variables in python itself
    >> without having a wrapper script.

    >
    > Yes, sure, you can set environment variables...
    >
    >> The wrapper script is now something like....
    >>
    >> #!/bin/bash
    >>
    >> export LD_LIBRARY_PATH="/some/thing/lib:$LD_LIBRARY_PATH"
    >> export LD_LIBRARY_PATH="/another/thing/lib:$LD_LIBRARY_PATH"
    >>
    >> export PATH="/some/thing/bin:$PATH"
    >> export PATH="/another/thing/bin:$PATH"
    >>
    >> python ./someScript.py

    >
    > ...but this won't work, I'm afraid.
    >
    > LD_LIBRARY_PATH is for the program loader / dynamic linker under Linux. This
    > thing is what is invoked _before_ the program is started, any later
    > modifications to the environment are ignored.


    In cases like yours I have sometimes written Python scripts that acted as
    their own wrapper:

    #!/usr/bin/env python

    import os, sys

    if 'LD_LIBRARY_PATH' in os.environ:
    lib_path = os.environ['LD_LIBRARY_PATH']
    if '/some/thing/lib' in lib_path and '/another/thing/lib' in lib_path:
    pass
    else:
    os.environ['LD_LIBRARY_PATH'] += ':/some/thing/lib:/another/thing/lib'
    os.execve(sys.argv[0], sys.argv, os.environ)
    else:
    os.environ['LD_LIBRARY_PATH'] = '/some/thing/lib:/another/thing/lib'
    os.execve(sys.argv[0], sys.argv, os.environ)

    os.environ['PATH'] = '/some/thing/bin:/another/thing/bin:' + os.environ['PATH']

    # At this point, you can import a module that depends
    # on LD_LIBRARY_PATH including /some/thing/lib
    #
    # Alternatively (and more clearly), you can replace the 'pass' above
    # by that import statement


    This code restarts Python if it has to modify os.environ['LD_LIBRARY_PATH'].

    If you try to single-step this code under pdb, you'll get as far as the
    os.execve() call. That call starts Python afresh, without a debugger.
    In other words, if you need to use pdb, you'll have to set the environment
    variables in the shell.

    > Similarly PATH, which tells the shell (e.g. bash) where to find executables.
    > If you need that to e.g. find 'python' itself, you're out of luck.
    > Otherwise, I believe Python itself doesn't use PATH, so you could set it
    > inside and any shells started from Python should pick it up.


    You don't have to restart Python if you modify to os.environ['PATH'],
    so that bit is easy.



    Hope this helps,

    -- HansM
     
    Hans Mulder, Dec 2, 2009
    #4
  5. On Dec 2, 4:12 pm, Ulrich Eckhardt <> wrote:
    > eric.frederich wrote:
    > > Is there a way to set up environment variables in python itself
    > > without having a wrapper script.

    >
    > Yes, sure, you can set environment variables...
    >
    > > The wrapper script is now something like....

    >
    > > #!/bin/bash

    >
    > > export LD_LIBRARY_PATH="/some/thing/lib:$LD_LIBRARY_PATH"
    > > export LD_LIBRARY_PATH="/another/thing/lib:$LD_LIBRARY_PATH"

    >
    > > export PATH="/some/thing/bin:$PATH"
    > > export PATH="/another/thing/bin:$PATH"

    >
    > > python ./someScript.py

    >
    > ...but this won't work, I'm afraid.
    >
    > LD_LIBRARY_PATH is for the program loader / dynamic linker under Linux. This
    > thing is what is invoked _before_ the program is started, any later
    > modifications to the environment are ignored.
    >
    > Similarly PATH, which tells the shell (e.g. bash) where to find executables.
    > If you need that to e.g. find 'python' itself, you're out of luck.
    > Otherwise, I believe Python itself doesn't use PATH, so you could set it
    > inside and any shells started from Python should pick it up.
    >
    > Uli
    >
    > --
    > Sator Laser GmbH
    > Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932



    I have had success in modifying LD_LIBRARY_PATH from within my Python
    code, to make sure that Python correctly loads DLL's from
    subdirectories of my project. (I believe the Python ended up calling
    CDll, or somesuch?)
     
    Jonathan Hartley, Dec 3, 2009
    #5
  6. On Dec 3, 3:13 pm, Jonathan Hartley <> wrote:
    > On Dec 2, 4:12 pm, Ulrich Eckhardt <> wrote:
    >
    >
    >
    > > eric.frederich wrote:
    > > > Is there a way to set up environment variables in python itself
    > > > without having a wrapper script.

    >
    > > Yes, sure, you can set environment variables...

    >
    > > > The wrapper script is now something like....

    >
    > > > #!/bin/bash

    >
    > > > export LD_LIBRARY_PATH="/some/thing/lib:$LD_LIBRARY_PATH"
    > > > export LD_LIBRARY_PATH="/another/thing/lib:$LD_LIBRARY_PATH"

    >
    > > > export PATH="/some/thing/bin:$PATH"
    > > > export PATH="/another/thing/bin:$PATH"

    >
    > > > python ./someScript.py

    >
    > > ...but this won't work, I'm afraid.

    >
    > > LD_LIBRARY_PATH is for the program loader / dynamic linker under Linux. This
    > > thing is what is invoked _before_ the program is started, any later
    > > modifications to the environment are ignored.

    >
    > > Similarly PATH, which tells the shell (e.g. bash) where to find executables.
    > > If you need that to e.g. find 'python' itself, you're out of luck.
    > > Otherwise, I believe Python itself doesn't use PATH, so you could set it
    > > inside and any shells started from Python should pick it up.

    >
    > > Uli

    >
    > > --
    > > Sator Laser GmbH
    > > Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

    >
    > I have had success in modifying LD_LIBRARY_PATH from within my Python
    > code, to make sure that Python correctly loads DLL's from
    > subdirectories of my project. (I believe the Python ended up calling
    > CDll, or somesuch?)



    Ahar! But of course, I was modifying os.environ, not setting the
    actual environment. I see.
     
    Jonathan Hartley, Dec 3, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Jollans
    Replies:
    0
    Views:
    152
    Thomas Jollans
    Aug 12, 2012
  2. Mark Lawrence
    Replies:
    0
    Views:
    221
    Mark Lawrence
    Aug 12, 2012
  3. Gelonida N
    Replies:
    0
    Views:
    132
    Gelonida N
    Aug 13, 2012
  4. Gelonida N
    Replies:
    0
    Views:
    128
    Gelonida N
    Aug 13, 2012
  5. bjlockie

    wrapper script calling anotherr script

    bjlockie, Sep 15, 2012, in forum: Perl Misc
    Replies:
    1
    Views:
    335
    bjlockie
    Sep 15, 2012
Loading...

Share This Page