Re: Environment Variables

Guido.van.Rossum@cwi.nl
Tue, 03 May 1994 13:31:38 +0200

> ( as root )
>
> import pwd,posix
> user=pwd.getpwnam( username )
> posix.setuid( user[2] )
> posix.chdir( user[-2] )
>
> Now *how* do you exec a users .profile so you can get the proper
> environment variables set and read back into python ?
>
> I've tried:
> posix.popen( "ksh -c '. ./profile ; env'" , 'r' ).readlines()
>
> ( and a few other variations ) but I get the OLD variables!

(And answering it in a later post:)

> My problem was that .profile doesn't set the variables
> I was interested in - they are set somewhere else.
>
> [ Which *still* leaves me with the problem of getting the
> correct values from somewhere, but the problem is NOT
> the one I thought it was! ]

This is a UNIX problem. I don't think there's a general solution --
at least your code will have to check the login shell to determine
whether to exec .profile or .login, and often these files will skip
certain sections when they aren't run interactively, etc. Maybe first
SETTING a few environment variables to the defaults that you get from
login (e.g. HOME, USER/LOGNAME, MAIL) and junking all others will have
more effect...

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
URL: <http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>