Environment Variable

V

Vivek Chaudhary

Is it possible to set an environment variable in python script whose
value is retained even after the script exits.

Doing the following creates an environment variable "name" which is
visible to only subprocesses created by os.system() and os.popen().

os.putvar("name", "vivek")

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"
 
G

Grant Edwards

Is it possible to set an environment variable in python script whose
value is retained even after the script exits.

No, not in Unix/Linux. In VMS I think there is.
 
S

Sybren Stuvel

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
 
D

Dennis Lee Bieber

No, not in Unix/Linux. In VMS I think there is.

And in the much maligned and ancient AmigaOS -- where there were
"local" (process specific) environment variables and global/system
environment variables. Locals were kept in process memory. Globals,
however, where kept in ENV: (for the VMS types, that would be equivalent
to a systemwide logical name into the file system). Each variable was a
separate text file...

--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top