Environment Variable

Sybren Stuvel sybrenUSE at YOURthirdtower.com.imagination
Mon Jul 11 16:59:32 EDT 2005


Vivek Chaudhary enlightened us with:
> Is it possible to set an environment variable in python script whose
> value is retained even after the script exits.

It is, if you have absolute control over the calling environment.

> Is it possible to somehow create this environment variable inside
> python script which will be avaibale even after the script exits. In
> otherwords, after I run my script, if I do a "echo $name" in my
> shell, it should return the value "vivek"

Here is an example Python script:

--------------------------------------------------------
import sys

name = sys.stdin.readline()
print "export name=%s" % name.strip()
--------------------------------------------------------

If you call it like this:

bash$ $(python examplescript)

It'll change the 'name' variable of your shell. It's not really a
generic nor an elegant way of doing this. Heck, it even depends on the
type of shell you're using. If it suits your needs, be happy ;-)

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



More information about the Python-list mailing list