473,549 Members | 2,750 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use os.putenv() ?

>>>
>>import os

os.environ['PATH']
'C:\\WINNT\\sys tem32;C:\\WINNT ;C:\\WINNT\\Sys tem32\\Wbem;%C: \\WINNT%\
\system32;%C:\\ WINNT%;%C:\\WIN NT%\\System32\\ Wbem'
>>>
os.putenv('PA TH', 'C:\\WINNT\\sys tem32')

os.environ['PATH']
'C:\\WINNT\\sys tem32;C:\\WINNT ;C:\\WINNT\\Sys tem32\\Wbem;%C: \\WINNT%\
\system32;%C:\\ WINNT%;%C:\\WIN NT%\\System32\\ Wbem'
>>>
What am I doing wrong? How do I change the value of an environment
variable?

Aug 30 '07 #1
4 16399
On Aug 30, 11:21 am, goo...@tyeon.co m wrote:
>import os
>os.environ['PATH']

'C:\\WINNT\\sys tem32;C:\\WINNT ;C:\\WINNT\\Sys tem32\\Wbem;%C: \\WINNT%\
\system32;%C:\\ WINNT%;%C:\\WIN NT%\\System32\\ Wbem'
>os.putenv('PAT H', 'C:\\WINNT\\sys tem32')
>os.environ['PATH']

'C:\\WINNT\\sys tem32;C:\\WINNT ;C:\\WINNT\\Sys tem32\\Wbem;%C: \\WINNT%\
\system32;%C:\\ WINNT%;%C:\\WIN NT%\\System32\\ Wbem'

What am I doing wrong? How do I change the value of an environment
variable?
What you are missing is that os.environ is only populated from the
global process environment at process startup.

If you update os.environ the changes will be pushed into the global
process environment as well. But if you use os.putenv() instead,
bypassing os.environ, the changes will not show in os.environ.

To confirm that the global process environment is being updated, use
os.getenv().

Graham

Aug 30 '07 #2
On Behalf Of go****@tyeon.co m
What am I doing wrong? How do I change the value of an
environment variable?
You'll have to go through the Windows registry. Please have a look at the
following recipe:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/55993

I also have my own routines based on that for getting and setting the path:

############### ###

import _winreg as winreg
import win32gui
import win32con

REG_KEY_PATH = r'SYSTEM\Curren tControlSet\Con trol\Session
Manager\Environ ment'

def set_path(pathva l):
"""Set the PATH environment variable"""

try:
reg = winreg.ConnectR egistry(None,
win32con.HKEY_L OCAL_MACHINE)
key = winreg.OpenKey( reg,
REG_KEY_PATH,
0,
win32con.KEY_AL L_ACCESS)

winreg.SetValue Ex(key,
'path',
0,
win32con.REG_EX PAND_SZ,
pathval)

win32gui.SendMe ssage(win32con. HWND_BROADCAST,
win32con.WM_SET TINGCHANGE,
0,
'Environment')

finally:
winreg.CloseKey (key)
winreg.CloseKey (reg)

def get_path():
"""Get the PATH environment variable"""
try:
reg = winreg.ConnectR egistry(None,
win32con.HKEY_L OCAL_MACHINE)
key = winreg.OpenKey( reg,
REG_KEY_PATH,
0,
win32con.KEY_AL L_ACCESS)

return winreg.QueryVal ueEx(key,
'path')[0]

finally:
winreg.CloseKey (key)
winreg.CloseKey (reg)

############### ###

Regards,
Ryan Ginstrom

Aug 30 '07 #3
T
On Aug 29, 9:50 pm, Graham Dumpleton <Graham.Dumple. ..@gmail.com>
wrote:
On Aug 30, 11:21 am, goo...@tyeon.co m wrote:
>>import os
>>os.environ['PATH']
'C:\\WINNT\\sys tem32;C:\\WINNT ;C:\\WINNT\\Sys tem32\\Wbem;%C: \\WINNT%\
\system32;%C:\\ WINNT%;%C:\\WIN NT%\\System32\\ Wbem'
>>os.putenv('PA TH', 'C:\\WINNT\\sys tem32')
>>os.environ['PATH']
'C:\\WINNT\\sys tem32;C:\\WINNT ;C:\\WINNT\\Sys tem32\\Wbem;%C: \\WINNT%\
\system32;%C:\\ WINNT%;%C:\\WIN NT%\\System32\\ Wbem'
What am I doing wrong? How do I change the value of an environment
variable?

What you are missing is that os.environ is only populated from the
global process environment at process startup.

If you update os.environ the changes will be pushed into the global
process environment as well. But if you use os.putenv() instead,
bypassing os.environ, the changes will not show in os.environ.

To confirm that the global process environment is being updated, use
os.getenv().

Graham
Can you tell me what I am still missing please?
>>import os

os.getenv('PA TH')
'C:\\WINNT\\sys tem32;C:\\WINNT ;C:\\WINNT\\Sys tem32\\Wbem;%C: \\WINNT%\
\system32;%C:\\ WINNT%;%C:\\WIN NT%\\System32\\ Wbem'
>>>
os.putenv('PA TH', 'C:\\WINNT\\sys tem32')

os.getenv('PA TH')
'C:\\WINNT\\sys tem32;C:\\WINNT ;C:\\WINNT\\Sys tem32\\Wbem;%C: \\WINNT%\
\system32;%C:\\ WINNT%;%C:\\WIN NT%\\System32\\ Wbem'
>>>
Aug 30 '07 #4
T
Thank you everyone!

Aug 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2468
by: Warren Oates | last post by:
I've been using putenv() to change the timezone in a script (my server is in CST, me in EST). My reading of the docs suggests that this changes the time zone environment variable for _the server_ for the duration of the script. Is this polite? If another account accesses the server at exactly the same time my script is running, is that...
1
4684
by: Bathroom_Monkey | last post by:
I've got a newb question about Daylight Savings Time (DST). First of all here is some server info: Apache 1.3.28 PHP 4.3.6 MySQL 3.23.49 Time is set to GMT My goal is to be able to accurately record website visits according to my local time (U.S. Central) every day of the year, no matter if I am
1
831
by: Greg Lindstrom | last post by:
Hello - I am trying to connect to an Oracle database on an HP-9000 via Python 2.3 and cx_Oracle. I have set the following in my python routine: os.putenv('ORACLE_HOME', '/u01/app/oracle') os.putenv('ORA_NLS', '/u01/app/oracle/product/9.2.0/ocommon/nls/admin/data') os.putenv('ORACLE_TERM', 'xterm') os.putenv('ORACLE_SID', 'test1')
4
3310
by: techme | last post by:
I am surprised at how little info there is on this topic. We converted to DB2 v8.1.1.72 painlessly a few months ago. For specific reasons, we stayed on 32-bit. Our OS is AIX 5.2 and our hardward is 64-bit P570 LPAR. I need to convert the DB2 DB to 64-bit now. Research suggests that this is a simple 'db2iupdt -w64 command' that will soft...
7
2617
by: dpugmire | last post by:
Is there a trick to getting putenv/getenv to work? I have csh script that calls a bunch of python programs and I'd like to use env variables as kind of a global variable that I can pass around to the pythong scripts. Thanks, Dave
2
2217
by: LeaFriend | last post by:
Hello. It's rainy day of winter in here. :( I install php on AIX, with OCI8 ext. WebServer is WebtoB, WAS is JEUS. There was a problem on compiling (oci.h is not found) and linking. (machine was 32bit system, but default oracle library path /tmax/oracle/client/lib is for 64bit) Anyway, i fixed Makefile and php work well with OCI on CLI....
4
2322
by: Yogi Watcher | last post by:
Hi, Recently I have observed some odd behavior of getenv and putenv function. I am developing some code that integrates with several other libraries. This program is not using MFC. It is plain C and C++ code. Some login and initialization function from one dll is setting an environment variable “MANUAL_LOGIN” to value “TRUE”. I...
8
3213
by: Michael B Allen | last post by:
Is the string returned by getenv guaranteed to be the same string supplied to putenv plus the offset of the variable name and equals sign? Because of API constraints I do not want to save a pointer to the string passed to putenv but I need to be able to free it later or I will have a memory leak. The following code demponstrates that with...
4
2939
by: ClownPleco | last post by:
I have read several posts about putenv is not Standard C. But I'm wondering if anyone knows if it is available in AIX. All of our other platforms (linux (32 and 64 bit), sgi, hpux, x86, darwin (intel and mac), interix and sun) behave properly for our application. We found that if we run this command prior to running the application, it...
0
7551
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, well explore What is ONU, What Is Router, ONU & Routers main...
0
7750
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7991
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...
1
7509
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...
0
7838
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...
0
6084
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5396
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...
0
3503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
790
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...

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.