Set an environment variable

Discussion in 'Python' started by Christian, Oct 20, 2005.

  1. Christian

    Christian Guest

    Another question from a not even newbie:

    In Unix you can set an environment variable with the command
    export PYTHONPATH
    but I would like to set the variable from at .py script.

    So my question is:
    How do I export an environment variable in a .py script?


    Thanks

    Chris
     
    Christian, Oct 20, 2005
    #1
    1. Advertising

  2. Christian

    the_crazy88 Guest

    Just use
    os.system("export PYTHONPATH = %s" %("your_pythonpath"))
     
    the_crazy88, Oct 20, 2005
    #2
    1. Advertising

  3. Christian

    Guest

    In Unix, you generally can't affect the environment of your parent program (in
    a broad sense, which includes environment variables, current working directory,
    opened files, effective user ID, etc).

    You have two basic choices to achieve an effect like this. First, you can
    start a subshell with the desired environment, something like (untested)
    os.environ['VARIABLE'] = 'value'
    os.system(os.environ['shell'])
    (commands like 'su' work in this way)

    Second, you can produce shell commands that have the effect of changing a
    shell's environment when they are executed. Something like:
    print "VARIABLE=value"
    To use this, the user must write something like
    eval `myscript.py`
    instead of just
    myscript.py
    this can typically be encapsulated in a shell alias or function.
    (commands like 'resize' (which sets the shell's idea of a terminal's size) work
    this way)

    Finally, you might be able to use an OS-specific interface like linux' ptrace
    to change the actual contents of a calling process's memory. I didn't
    immediately find an example of this, but on Linux it would consist of using
    PTRACE_PEEKDATA and PTRACE_POKEDATA to locate the environment in another
    process and modify it. The ptrace interface is not available from any standard
    Python module, and isn't portable anyway. (though my manpage actually says
    'CONFORMING TO SVr4, ..., X/OPEN, BSD 4.3' so maybe ptrace is more portable
    than I believed)

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFDV4sxJd01MZaTXX0RAj4aAJ41BlvbrZxLNrhm+Ea5bgMyZOkVwQCglsLw
    wFB6iqRqw3FRznozCpV5qh0=
    =MqJa
    -----END PGP SIGNATURE-----
     
    , Oct 20, 2005
    #3
  4. Christian

    Christian Guest

    Thanks Jeff and the crazy 88.
     
    Christian, Oct 20, 2005
    #4
  5. Christian

    Eric Brunel Guest

    On 20 Oct 2005 01:58:44 -0700, the_crazy88 <> wrote:

    > Just use
    > os.system("export PYTHONPATH = %s" %("your_pythonpath"))


    .... except it won't work: os.system will execute the command in a new process, so the environment variable change will only be visible in *this* process. Since you don't do anything else, the environment variable change will never be seen by anyone.

    As for the OP's question, the short answer is "you can't": the Python interpreter will always be executed in a different process than the calling shell, and environment variable changes *never* impact the calling process on Unix.

    The closest thing you can do is that:

    -myScript.py--------------------------------------
    print 'export MY_VARIABLE=value'
    --------------------------------------------------

    -myScript.sh--------------------------------------
    python myScript.py > /tmp/chgvars.sh
    .. /tmp/chgvars.sh
    --------------------------------------------------

    This is quite ugly: you write the shell commands changing the environment variables to a file, then "source" this file in the calling shell. But this is probably the best way to do what you want.

    HTH
    --
    python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"
     
    Eric Brunel, Oct 20, 2005
    #5
  6. On 2005-10-20, the_crazy88 <> wrote:

    > os.system("export PYTHONPATH = %s" %("your_pythonpath"))


    No, that won't work.

    That will set the environment variable in the shell spawned by
    the os.system command. That shell will then immediately exit,
    leaving the caller's environment unchanged.

    --
    Grant Edwards grante Yow! Are you mentally here
    at at Pizza Hut??
    visi.com
     
    Grant Edwards, Oct 20, 2005
    #6
  7. Grant Edwards, Oct 20, 2005
    #7
  8. Christian

    Mike Meyer Guest

    "Eric Brunel" <> writes:
    > -myScript.py--------------------------------------
    > print 'export MY_VARIABLE=value'
    > --------------------------------------------------
    >
    > -myScript.sh--------------------------------------
    > python myScript.py > /tmp/chgvars.sh
    > . /tmp/chgvars.sh


    It's simpler to use eval and command substitution:

    eval $(python myScript.py)

    <mike

    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Oct 20, 2005
    #8
  9. Christian

    Christian Guest

    >
    > The closest thing you can do is that:
    >
    > -myScript.py--------------------------------------
    > print 'export MY_VARIABLE=value'
    > --------------------------------------------------
    >
    > -myScript.sh--------------------------------------
    > python myScript.py > /tmp/chgvars.sh
    > . /tmp/chgvars.sh
    > --------------------------------------------------


    Can I write a .py script that calls a .sh script that executes the
    export command and then calls another .py script (and how would the
    first .py script look)?

    That would be much more what is my basic problem.


    Thanks

    Chris
     
    Christian, Oct 21, 2005
    #9
  10. Christian wrote:

    > Can I write a .py script that calls a .sh script that executes the
    > export command and then calls another .py script (and how would the
    > first .py script look)?


    No, the shell script that the Python program would invoke would be a
    different process and so commands executed in it would have no effect on
    the state of another.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    Success and failure are equally disastrous.
    -- Tennessee Williams
     
    Erik Max Francis, Oct 21, 2005
    #10
  11. Christian

    Christian Guest

    Erik Max Francis wrote:
    > Christian wrote:
    >
    >> Can I write a .py script that calls a .sh script that executes the
    >> export command and then calls another .py script (and how would the
    >> first .py script look)?

    >
    > No, the shell script that the Python program would invoke would be a
    > different process and so commands executed in it would have no effect on
    > the state of another.
    >


    So executing an .sh script that calls a .py script works different when
    executed from a command promt than when executed from a starter .py script?
     
    Christian, Oct 21, 2005
    #11
  12. Mike Meyer enlightened us with:
    > It's simpler to use eval and command substitution:
    >
    > eval $(python myScript.py)


    This looks like the best solution to me.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Oct 21, 2005
    #12
  13. Christian

    Steve Holden Guest

    Christian wrote:
    >>The closest thing you can do is that:
    >>
    >>-myScript.py--------------------------------------
    >>print 'export MY_VARIABLE=value'
    >>--------------------------------------------------
    >>
    >>-myScript.sh--------------------------------------
    >>python myScript.py > /tmp/chgvars.sh
    >>. /tmp/chgvars.sh
    >>--------------------------------------------------

    >
    >
    > Can I write a .py script that calls a .sh script that executes the
    > export command and then calls another .py script (and how would the
    > first .py script look)?
    >
    > That would be much more what is my basic problem.
    >

    You can do what you suggest without shell scripting, unless I
    misunderstand your intention: just set the environment variables you
    want your Python script to see and then run it using os.system():

    ::::::::::::::
    one.py
    ::::::::::::::
    import os
    os.environ['STEVE'] = "You are the man"
    os.system("python two.py")
    print "Ran one"
    ::::::::::::::
    two.py
    ::::::::::::::
    import os
    print "STEVE is", os.environ['STEVE']
    print "Ran two"
    [sholden@headrat tmp]$ python one.py
    STEVE is You are the man
    Ran two
    Ran one
    [sholden@headrat tmp]$

    Hope this helps.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/
     
    Steve Holden, Oct 21, 2005
    #13
  14. On 2005-10-21, Christian wrote:
    > Erik Max Francis wrote:
    >> Christian wrote:
    >>
    >>> Can I write a .py script that calls a .sh script that executes the
    >>> export command and then calls another .py script (and how would the
    >>> first .py script look)?

    >>
    >> No, the shell script that the Python program would invoke would be a
    >> different process and so commands executed in it would have no effect on
    >> the state of another.
    >>

    >
    > So executing an .sh script that calls a .py script works different when
    > executed from a command promt than when executed from a starter .py script?


    No; it's always the same: an environment variable will only be
    effective in the process in which it is set, and its children.

    When you call another program, whether it's a shell script, python
    script, or binary executable, you are starting a new process.
    Environment variables set in that process will not affect its
    parent (i.e., the process that called it).

    --
    Chris F.A. Johnson <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>
     
    Chris F.A. Johnson, Oct 21, 2005
    #14
  15. Christian

    Christian Guest

    Steve Holden wrote:

    > ::::::::::::::
    > one.py
    > ::::::::::::::
    > import os
    > os.environ['STEVE'] = "You are the man"
    > os.system("python two.py")
    > print "Ran one"
    > ::::::::::::::
    > two.py
    > ::::::::::::::
    > import os
    > print "STEVE is", os.environ['STEVE']
    > print "Ran two"
    > [sholden@headrat tmp]$ python one.py
    > STEVE is You are the man
    > Ran two
    > Ran one
    > [sholden@headrat tmp]$
    >
    > Hope this helps.
    >
    > regards
    > Steve


    Thanks Steve, you're quite right, you are the man. And thanks to all the
    rest of you for your kind help and patient understanding. I have learned
    quite a lot and is about to consider my self advanced to the status of
    Python newbie.

    So here is my final question:
    Do I call the .sh script with a .py script like this:

    os.system("/path/to/the/script/startupscript.sh")
     
    Christian, Oct 21, 2005
    #15
  16. Christian

    Steve Holden Guest

    Christian wrote:
    > Steve Holden wrote:
    >
    >
    >>::::::::::::::
    >>one.py
    >>::::::::::::::
    >>import os
    >>os.environ['STEVE'] = "You are the man"
    >>os.system("python two.py")
    >>print "Ran one"
    >>::::::::::::::
    >>two.py
    >>::::::::::::::
    >>import os
    >>print "STEVE is", os.environ['STEVE']
    >>print "Ran two"
    >>[sholden@headrat tmp]$ python one.py
    >>STEVE is You are the man
    >>Ran two
    >>Ran one
    >>[sholden@headrat tmp]$
    >>
    >>Hope this helps.
    >>
    >>regards
    >> Steve

    >
    >
    > Thanks Steve, you're quite right, you are the man. And thanks to all the
    > rest of you for your kind help and patient understanding. I have learned
    > quite a lot and is about to consider my self advanced to the status of
    > Python newbie.
    >
    > So here is my final question:
    > Do I call the .sh script with a .py script like this:
    >
    > os.system("/path/to/the/script/startupscript.sh")


    Time you answered your own questions by trying things at the interactive
    interpreter prompt!

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/
     
    Steve Holden, Oct 21, 2005
    #16
  17. Christian

    Christian Guest

    Steve Holden wrote:

    > Time you answered your own questions by trying things at the interactive
    > interpreter prompt!
    >
    > regards
    > Steve


    Right again, Steve.

    Thanks
     
    Christian, Oct 21, 2005
    #17
  18. On 2005-10-21, Christian <> wrote:
    >>
    >> The closest thing you can do is that:
    >>
    >> -myScript.py--------------------------------------
    >> print 'export MY_VARIABLE=value'
    >> --------------------------------------------------
    >>
    >> -myScript.sh--------------------------------------
    >> python myScript.py > /tmp/chgvars.sh
    >> . /tmp/chgvars.sh
    >> --------------------------------------------------


    Bullshit. Are people being intentionally misleading??

    > Can I write a .py script that calls a .sh script that executes the
    > export command and then calls another .py script (and how would the
    > first .py script look)?


    Good grief, that's ugly. Just use os.putenv().

    > That would be much more what is my basic problem.


    And even Google knows the correct answer

    http://www.google.com/search?hl=en&lr=&q=python set environment variable

    Follow the first hit.

    --
    Grant Edwards grante Yow! TONY RANDALL! Is YOUR
    at life a PATIO of FUN??
    visi.com
     
    Grant Edwards, Oct 21, 2005
    #18
  19. Christian

    Mike Meyer Guest

    Grant Edwards <> writes:

    > On 2005-10-21, Christian <> wrote:
    >>>
    >>> The closest thing you can do is that:
    >>>
    >>> -myScript.py--------------------------------------
    >>> print 'export MY_VARIABLE=value'
    >>> --------------------------------------------------
    >>>
    >>> -myScript.sh--------------------------------------
    >>> python myScript.py > /tmp/chgvars.sh
    >>> . /tmp/chgvars.sh
    >>> --------------------------------------------------

    >
    > Bullshit. Are people being intentionally misleading??


    No. Are you being intentionally - never mind.

    > And even Google knows the correct answer
    >
    > http://www.google.com/search?hl=en&lr=&q=python set environment variable
    >
    > Follow the first hit.


    The first hit is basically the solution presented above translated
    from Unix to Windows: your python script writes the appropriate shell
    commands into a file, then you get the command processor to process
    that file. The Windows version carries this a step further by wrapping
    it all up in a script to make it easy to run, but that's the only real
    difference. Maybe the results order has changed since you looked?

    Watch the recipe - I may add a Unix/sh solution.

    <mike

    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Oct 21, 2005
    #19
  20. On 2005-10-21, Mike Meyer <> wrote:

    >>>> The closest thing you can do is that:
    >>>>
    >>>> -myScript.py--------------------------------------
    >>>> print 'export MY_VARIABLE=value'
    >>>> --------------------------------------------------
    >>>>
    >>>> -myScript.sh--------------------------------------
    >>>> python myScript.py > /tmp/chgvars.sh
    >>>> . /tmp/chgvars.sh
    >>>> --------------------------------------------------

    >>
    >> Bullshit. Are people being intentionally misleading??

    >
    > No. Are you being intentionally - never mind.


    Well, yes, probably.

    >> And even Google knows the correct answer
    >>
    >> http://www.google.com/search?hl=en&lr=&q=python set environment variable
    >>
    >> Follow the first hit.


    My bad. I got links mixed up -- it wasn't the first one, it
    was this one:

    http://www.faqts.com/knowledge_base/view.phtml/aid/3298

    There are two almost-equivalent tirival answers:

    os.environment['foo'] = 'bar'

    os.putenv('foo','bar')

    I don't get why people seem to be obfuscating things with
    multiple layers of shells or writing shell commands to to a
    file and executing them.

    > Maybe the results order has changed since you looked?


    No, I mixed them up.

    My point: the OP wanted to know how to export an environment
    variable to a child process. Either of the lines of code above
    will do that, so what's with all the shellular shenanigans?

    --
    Grant Edwards grante Yow! Youth of today! Join
    at me in a mass rally
    visi.com for traditional mental
    attitudes!
     
    Grant Edwards, Oct 21, 2005
    #20
    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. Tom
    Replies:
    0
    Views:
    454
  2. Fuzzyman

    Set Windows Environment Variable

    Fuzzyman, Mar 30, 2006, in forum: Python
    Replies:
    6
    Views:
    8,651
    Fuzzyman
    Mar 30, 2006
  3. Replies:
    5
    Views:
    685
  4. chandan
    Replies:
    1
    Views:
    383
    Naraendirakumar R.R.
    Jan 8, 2008
  5. bill

    set DOS environment variable

    bill, Oct 2, 2008, in forum: Python
    Replies:
    1
    Views:
    438
    Dan Upton
    Oct 2, 2008
Loading...

Share This Page