Discussion:
newbie: Windows os.getenv(...
Pettersen, Bjorn S
2003-10-20 11:47:00 UTC
Permalink
From: Martin [mailto:info12 at webmail.link-m.de]
Hi there,
i?d like to use environment variables within an python script.
I can read with
t1 = os.getenv("temp")
but not
d1 = os.getenv("%date%")
date doesn't seem to be an environment variable (check output of the 'set' command in a dos window or hit Windows+break -> Advanced -> environment variables in a recent version of windows). The fact that 'echo %date%' gives a result is "interesting"...
Also I cannot change environment variables with
os.putenv("any" , "something")
try

os.environ['any'] = 'something'

hth,
-- bjorn
Peter Hansen
2003-10-20 13:37:38 UTC
Permalink
i?d like to use environment variables within an python script.
I can read with
t1 = os.getenv("temp")
but not
d1 = os.getenv("%date%")
As Diez said, %DATE% is a purely DOS convention with no meaning in
Python. It's a variable substitution technique using the DATE
environment variable.
Also I cannot change environment variables with
os.putenv("any" , "something")
Do you have a clue?
You cannot, if you mean you want the changes to persist after your
script has completed. On the other hand, there are techniques you
can use to work around the problem. Try searching the mailing list
or newsgroup archives (e.g. with groups.google.com) for the several
past discussions of this and the answers.

-Peter
Martin
2003-10-21 09:03:34 UTC
Permalink
Thanks to You all,

I found lots in the archieves about that topic.

I will solve my task without exporting OS-variables.




Peter Hansen et al. said:
[..]
Post by Peter Hansen
You cannot, if you mean you want the changes to persist after your
script has completed. On the other hand, there are techniques you
can use to work around the problem. Try searching the mailing list
or newsgroup archives (e.g. with groups.google.com) for the several
past discussions of this and the answers.
-Peter
Thank You all
Martin
Diez B. Roggisch
2003-10-20 08:57:26 UTC
Permalink
Hi,
i?d like to use environment variables within an python script.
I can read with
t1 = os.getenv("temp")
but not
d1 = os.getenv("%date%")
%<name>% means that the command.com (or whatever its called under >w2k) is
supposed to substitute the expression with the value of the variable
<name>. E.g:

set PATH=bar
set PATH=%PATH%;foo
echo %PATH%
->bar;foo

However, in python-scripts the command.com isn't going to replace anything,
as its not running your script.
Also I cannot change environment variables with
os.putenv("any" , "something")
I have very limited windows expirience, but under unix a child process
(thats what your python-interpreter is) inherits the environment of its
parent by getting an actual copy. Otherwise it wouldn't be possible to
alter env-variables while other child-processes are running without
affecting these. So you can't overwrite the env of your parent process.

Maybe there is a way, but I have to admit I don't know how.

Regards,

Diez
Martin
2003-10-20 08:32:05 UTC
Permalink
Hi there,

i?d like to use environment variables within an python script.
I can read with
t1 = os.getenv("temp")
but not
d1 = os.getenv("%date%")
Also I cannot change environment variables with
os.putenv("any" , "something")

Do you have a clue?
thank you

Martin
Matt Gerrans
2003-10-21 01:39:10 UTC
Permalink
...but not
d1 = os.getenv("%date%")
DATE isn't really an environment variable (like TIME, CD, etc.), it is just
a little convenience that the command interpreter gives you for making batch
file ("shell script") tasks (such as logging) a little less difficult. You
could get the fake date "environment variable" from the command interpreter
like this:

reader = os.popen( 'cmd /c echo %date%', 'r' )
datestring = reader.read().strip()

But I think it would probably make a lot more sense to just use Python's
time module, which has a lot more to offer.
Also I cannot change environment variables with
os.putenv("any" , "something")
As for setting environment variables, there are (at least) two things to
consider:

1) Do you want it to persist for the "current session" after your script
runs? This is a toughie. I had a little module to do this back in the DOS
days, that involved messing with the PSP, but that doesn't fly in NT/XP/2K!
:-( The closest you can come is to have your script write a batch that is
subsequently called by the caller.

2) If you want to modify the current user's or the system's environment
permanently, but don't care so much about the "current session" then you
tweak the registry then broadcast a system message (to tell other apps (most
of whom are not paying attention!) that you made the change). If you need
more info on this and the searching suggested by Peter Hansen elsewhere in
this thread doesn't turn it up, post back here and I'll dump the full (gory)
details.
Do you have a clue?
Hey, you don't have to rub it in!

Loading...