Setting environment variables

Peter Hansen peter at engcorp.com
Thu Sep 18 15:07:37 CEST 2003


Erik Max Francis wrote:
> 
> Sami Viitanen wrote:
> 
> > Is there any other way to set variables than os.putenv().
> >
> > Putenv doesn't actually put any values to actual system variables..
> >
> > I'm trying to set CVSEDITOR variable automatically from script
> > so that user wouldn't have to set that him/herself. CVS can't use
> > variable
> > set with putenv().
> 
> Yep, and this is a feature, at least in Unix operating systems.
> Subshells cannot directly affect the operating environment of parent
> shells, and shouldn't try.  (One can do such things indirectly, but they
> require the cooperation of the parent shell, such as sourcing the output
> of a program, which is not an uncommon approach to the problem.)  I
> don't know if there's any way for Windows applications to affect parent
> environments, but I doubt it.

Short of bizarre hacks that are generally unacceptable, no there's no
way to do it even under Windows.

Under both operating systems, the best approach is to contrive a way
for another script to be executed *after* the application runs.  That
other script either accesses data that is generated by the application,
or is actually itself generated by the application as needed, and because
it's a script (e.g. .BAT or .CMD for Windows, .sh etc. for Unix) it 
is able to change the shell.  (And I know you already knew this, EMF, 
I was of course responding for others. :-)

-Peter




More information about the Python-list mailing list