Create a variable "on the fly"

Steven Bethard steven.bethard at gmail.com
Thu Jul 28 11:06:18 EDT 2005


Paul D.Smith wrote:
> 2. A simple Python config which searches for all shell environment variables
> named "MY_..." and instantiates then as Python variables.

my_vars = dict((k, v)
                for k, v in os.environ.iteritems()
                if k.startswith('MY_'))
globals().update(my_vars)

If you ever refactor the code, I'd suggest writing a single 
configuration file instead of setting environment variables, and reading 
that configuration file in each script that needs it.  Generally I've 
found that relying on environment variables being set is hard to 
maintain and a real pain when you have to move to a new system.

STeVe



More information about the Python-list mailing list