Setting environment variables

Discussion in 'Python' started by Sami Viitanen, Sep 18, 2003.

  1. Hello,

    Is there any other way to set variables than os.putenv().

    Putenv doesn't actually put any values to actual system variables..

    I'm trying to set CVSEDITOR variable automatically from script
    so that user wouldn't have to set that him/herself. CVS can't use variable
    set with putenv().


    Thanks in advance.
     
    Sami Viitanen, Sep 18, 2003
    #1
    1. Advertising

  2. Sami Viitanen wrote:

    > Is there any other way to set variables than os.putenv().
    >
    > Putenv doesn't actually put any values to actual system variables..
    >
    > I'm trying to set CVSEDITOR variable automatically from script
    > so that user wouldn't have to set that him/herself. CVS can't use
    > variable
    > set with putenv().


    Yep, and this is a feature, at least in Unix operating systems.
    Subshells cannot directly affect the operating environment of parent
    shells, and shouldn't try. (One can do such things indirectly, but they
    require the cooperation of the parent shell, such as sourcing the output
    of a program, which is not an uncommon approach to the problem.) I
    don't know if there's any way for Windows applications to affect parent
    environments, but I doubt it.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ Perfect situations must go wrong
    \__/ Florence, _Chess_
     
    Erik Max Francis, Sep 18, 2003
    #2
    1. Advertising

  3. Sami Viitanen

    Peter Hansen Guest

    Erik Max Francis wrote:
    >
    > Sami Viitanen wrote:
    >
    > > Is there any other way to set variables than os.putenv().
    > >
    > > Putenv doesn't actually put any values to actual system variables..
    > >
    > > I'm trying to set CVSEDITOR variable automatically from script
    > > so that user wouldn't have to set that him/herself. CVS can't use
    > > variable
    > > set with putenv().

    >
    > Yep, and this is a feature, at least in Unix operating systems.
    > Subshells cannot directly affect the operating environment of parent
    > shells, and shouldn't try. (One can do such things indirectly, but they
    > require the cooperation of the parent shell, such as sourcing the output
    > of a program, which is not an uncommon approach to the problem.) I
    > don't know if there's any way for Windows applications to affect parent
    > environments, but I doubt it.


    Short of bizarre hacks that are generally unacceptable, no there's no
    way to do it even under Windows.

    Under both operating systems, the best approach is to contrive a way
    for another script to be executed *after* the application runs. That
    other script either accesses data that is generated by the application,
    or is actually itself generated by the application as needed, and because
    it's a script (e.g. .BAT or .CMD for Windows, .sh etc. for Unix) it
    is able to change the shell. (And I know you already knew this, EMF,
    I was of course responding for others. :)

    -Peter
     
    Peter Hansen, Sep 18, 2003
    #3
  4. Sami Viitanen

    David C. Fox Guest

    Erik Max Francis wrote:
    > Sami Viitanen wrote:
    >
    >
    >>Is there any other way to set variables than os.putenv().
    >>
    >>Putenv doesn't actually put any values to actual system variables..
    >>
    >>I'm trying to set CVSEDITOR variable automatically from script
    >>so that user wouldn't have to set that him/herself. CVS can't use
    >>variable
    >>set with putenv().

    >
    >
    > Yep, and this is a feature, at least in Unix operating systems.
    > Subshells cannot directly affect the operating environment of parent
    > shells, and shouldn't try. (One can do such things indirectly, but they
    > require the cooperation of the parent shell, such as sourcing the output
    > of a program, which is not an uncommon approach to the problem.) I
    > don't know if there's any way for Windows applications to affect parent
    > environments, but I doubt it.
    >


    In [t]csh:

    alias cvsscript "setenv CVSEDITOR `python myscript.py`"
    cvsscript

    or miscellaneous variants in other shells. Of course, you can't use
    cvsscript from within another subshell, or it will only set the variable
    in that subshell.

    David
     
    David C. Fox, Sep 18, 2003
    #4
  5. Sami Viitanen wrote:
    > Hello,
    >
    > Is there any other way to set variables than os.putenv().
    >
    > Putenv doesn't actually put any values to actual system variables..
    >
    > I'm trying to set CVSEDITOR variable automatically from script
    > so that user wouldn't have to set that him/herself. CVS can't use variable
    > set with putenv().


    You could do the equivalent of:
    Paddy@maximillian ~ : echo $SHELL
    /bin/bash
    Paddy@maximillian ~ : unset foo
    Paddy@maximillian ~ : eval `python -c 'print "foo=Hello;export foo"'`
    Paddy@maximillian ~ : echo $foo
    Hello
    Paddy@maximillian ~ :

    I use the following for setting up quite complex environments at work:
    http://modules.sourceforge.net/

    Pad.
     
    Donald 'Paddy' McCarthy, Sep 18, 2003
    #5
    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. Rick Kasten

    Setting Environment Variables

    Rick Kasten, Jul 7, 2004, in forum: Perl
    Replies:
    2
    Views:
    607
    Howard
    Jul 7, 2004
  2. =?Utf-8?B?YnNmbDQ0?=
    Replies:
    0
    Views:
    496
    =?Utf-8?B?YnNmbDQ0?=
    May 12, 2004
  3. Steve
    Replies:
    6
    Views:
    24,126
    lightsedge
    May 1, 2007
  4. Ulf Meinhardt
    Replies:
    0
    Views:
    572
    Ulf Meinhardt
    Oct 13, 2004
  5. Replies:
    5
    Views:
    676
Loading...

Share This Page