newbie: Windows os.getenv(...

M

Martin

Hi there,

i´d like to use environment variables within an python script.
I can read with
Also I cannot change environment variables with
os.putenv("any" , "something")

Do you have a clue?
thank you

Martin
 
D

Diez B. Roggisch

Hi,
i´d like to use environment variables within an python script.
I can read with

%<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
 
P

Peter Hansen

Martin said:
i´d like to use environment variables within an python script.
I can read with

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
 
M

Matt Gerrans

Martin said:
...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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top