By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,531 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

Environment Variable

P: n/a
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"
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2005-07-11, Vivek Chaudhary <vi*******@yahoo.com> wrote:
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.

--
Grant Edwards grante Yow! I think my CAREER
at is RUINED!!
visi.com
Jul 21 '05 #2

P: n/a
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
Jul 21 '05 #3

P: n/a
On Mon, 11 Jul 2005 20:23:07 -0000, Grant Edwards <gr****@visi.com>
declaimed the following in comp.lang.python:
On 2005-07-11, Vivek Chaudhary <vi*******@yahoo.com> wrote:
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.


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...

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 21 '05 #4

P: n/a
[Vivek Chaudhary]
Is it possible to set an environment variable in python script whose
value is retained even after the script exits.


There is an indirect approach:
http://aspn.activestate.com/ASPN/Coo.../Recipe/159462
Raymond

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.