473,657 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2215
On 2005-07-11, Vivek Chaudhary <vi*******@yaho o.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.readl ine()
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.co m>
declaimed the following in comp.lang.pytho n:
On 2005-07-11, Vivek Chaudhary <vi*******@yaho o.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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.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
20972
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 is: How do I export an environment variable in a .py script?
13
3015
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 *)malloc(1028); strcpy(var, "VAR="); strcat(var, argv);
1
2454
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 program's application directory, along with the DLL that contains the generic code that they both call. My question is, is there any way of modifying the environment variable permanently with the setup project, such that the user has got the...
4
39084
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 GetEnvironmentVariable("path"). However, I don't know yet how to append strings to the path variable. Any help is appreciated. Thanks a lot.
3
5682
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 machine, nothing makes it work. Does INCLUDE variable until work? I'm using WinXP SP1 and VC 7.1. Thanks,
6
5331
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 = Environment.GetEnvironmentVariable("FOO");
0
1974
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 Environment Variable to the Registry and send a broadcast message to all open windows to update their environment. When I run this *.exe out of a cmd.exe, it will not update the environment of that shell. The new Environment Variable shows up
0
1286
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 String 3) first user user opens app at 9:00 and runs code that results in setting the variable strSample to "abc"
4
7521
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 variable is not in the os.environ dictionary. I know that cron runs in a subshell that does not have all of the normally set environment variables. HOSTNAME is not one of those variables, it is set even in cron's subshell. Why doesn't python get...
4
16579
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 there any way we can create user level environment variable for User2 using C# .Net 2.0. If we use Environment.SetEnvironmentVariable(envName, envValue, EnvironmentVariableTarget.User) then it will create the environment variable for the...
0
8385
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8723
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.