[Tutor] conventions for establishing and saving default valuesfor variables

Steven D'Aprano steve at pearwood.info
Thu Jun 17 01:19:58 CEST 2010


On Thu, 17 Jun 2010 03:25:03 am Alan Gauld wrote:
> "R. Alan Monroe" <amonroe at columbus.rr.com> wrote
>
> >> directory, I suppose. Under windows, probably the registry.
> >> There's the _winreg and configparser modules.
> >
> > Consider using the %USERPROFILE% environment variable rather than
> > the
> > registry.
>
> How would that work? That is just a single variable that points
> to the users Settings folder. Effectively their home directory in
> unix terms, so you could store the config file there. But
> you couldn't store the kind of data you would store in the
> registry?
>
> I'm confused.

Environment variables are not permanent storage. They only exist in RAM 
unless you take steps to recreate them from permanent storage. Under 
Linux, for example, environment variables are recreated each time you 
log in after being read from a config file, such as .bashrc. My .bashrc 
includes:

export PYTHONPATH=/home/steve/python/
export PYTHONSTARTUP=/home/steve/python/startup.py

So even if you wrote the values you cared about to one or more 
environment variable, they would disappear as soon as the user logged 
out or rebooted.

It gets worse... Python includes the command os.putenv to set 
environment variables, but they are only set for subprocesses of the 
Python process that sets them. So for example, I can do this:

>>> os.putenv('myenvvar', 'value')
>>> os.system('echo $myenvvar')
value
0

but if I turn to another shell (not a subshell) it doesn't exist:

[steve at sylar ~]$ echo $myenvvar

[steve at sylar ~]$


See also this:
http://code.activestate.com/recipes/159462-how-to-set-environment-variables/


So unless I've missed something exceedingly subtle, writing to 
environment variables is not a solution to the problem of saving 
default values. It's not even part of a solution.



-- 
Steven D'Aprano


More information about the Tutor mailing list