473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Environment Variable

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
4 2198
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
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
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
[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Christian | last post by:
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...
13
by: Jimmy Cracker | last post by:
Is it completely impossible in UNIX to push an environment variable to the parent shell? I would like to do something like this: main(int argc, char *argv) { char *var; var = (char...
1
by: Bonj | last post by:
Hi My application installs a front-end GUI, which runs code when buttons are clicked. It also installs a command-line utility, that is a console application. They are both installed to the...
4
by: | last post by:
Hi all, I am trying to append a certain string to the PATH environment variable programmatically. I am able to read what is in the variable using the System.Environment method...
3
by: Strauss | last post by:
Hi: I'm setting the INCLUDE environment variable, but VC is not searching it for header files (I've checked it with filemon). I've tried with user variable, machine variable, rebooted the...
6
by: yaron | last post by:
Hi, my application use environment variable, let call it FOO. how can i add the FOO environment variable to my project or my solution, so the line string foo =...
0
by: Joe HM | last post by:
Hello - I am putting together a little ConsoleApplication that is supposed to check for an Environment Variable and create it if it does not exist. I found some code that will add a new...
0
by: mirandacascade | last post by:
Apologies in advance for what I'm guessing may be a trivial question. Assume the following: 1) multi-user environment 2) standard (i.e. not a class module) module with: Public strSample as...
4
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test, oddly under cron the HOSTNAME environment...
4
by: vkbishnoi | last post by:
I need to create an environment variable for another local user in windows. For example, say suppose there are 2 users in the system (User1 and User2). Currently User1 is logged in the system. Now Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.