473,326 Members | 2,148 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,326 software developers and data experts.

How to use os.putenv() ?

>>>
>>import os

os.environ['PATH']
'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32 \\Wbem;%C:\\WINNT%\
\system32;%C:\\WINNT%;%C:\\WINNT%\\System32\\Wbem'
>>>
os.putenv('PATH', 'C:\\WINNT\\system32')

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

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

'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32 \\Wbem;%C:\\WINNT%\
\system32;%C:\\WINNT%;%C:\\WINNT%\\System32\\Wbem'
>os.putenv('PATH', 'C:\\WINNT\\system32')
>os.environ['PATH']

'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32 \\Wbem;%C:\\WINNT%\
\system32;%C:\\WINNT%;%C:\\WINNT%\\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.com
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\CurrentControlSet\Control\Session
Manager\Environment'

def set_path(pathval):
"""Set the PATH environment variable"""

try:
reg = winreg.ConnectRegistry(None,
win32con.HKEY_LOCAL_MACHINE)
key = winreg.OpenKey(reg,
REG_KEY_PATH,
0,
win32con.KEY_ALL_ACCESS)

winreg.SetValueEx(key,
'path',
0,
win32con.REG_EXPAND_SZ,
pathval)

win32gui.SendMessage(win32con.HWND_BROADCAST,
win32con.WM_SETTINGCHANGE,
0,
'Environment')

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

def get_path():
"""Get the PATH environment variable"""
try:
reg = winreg.ConnectRegistry(None,
win32con.HKEY_LOCAL_MACHINE)
key = winreg.OpenKey(reg,
REG_KEY_PATH,
0,
win32con.KEY_ALL_ACCESS)

return winreg.QueryValueEx(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.com wrote:
>>import os
>>os.environ['PATH']
'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32 \\Wbem;%C:\\WINNT%\
\system32;%C:\\WINNT%;%C:\\WINNT%\\System32\\Wbem'
>>os.putenv('PATH', 'C:\\WINNT\\system32')
>>os.environ['PATH']
'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32 \\Wbem;%C:\\WINNT%\
\system32;%C:\\WINNT%;%C:\\WINNT%\\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('PATH')
'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32 \\Wbem;%C:\\WINNT%\
\system32;%C:\\WINNT%;%C:\\WINNT%\\System32\\Wbem'
>>>
os.putenv('PATH', 'C:\\WINNT\\system32')

os.getenv('PATH')
'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32 \\Wbem;%C:\\WINNT%\
\system32;%C:\\WINNT%;%C:\\WINNT%\\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
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_...
1
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...
1
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')...
4
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...
7
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...
2
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...
4
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...
8
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...
4
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.