Changing environment variables into python variables

Thomas Wouters thomas at xs4all.nl
Fri Jun 4 01:27:28 EDT 1999


On Fri, Jun 04, 1999 at 02:52:16AM +0000, sragsdale at my-deja.com wrote:

> I'm writing a script that needs to import environment variables into
> python as standard variables.  I.E. if the environment variable USER is
> set to 'soren', after my program starts up and does its thing I want
> there to be a variable named USER with a value of 'soren'.
> 
> I can write this program with little effort in perl:

spare me the whitespace-impaired comic-character-cussword-language, pleeease
:-)

> When I try to write this in python, I have this error:

> -------------------------------
> import posix
> for var in posix.environ.keys():
>     command = var+" = \""+posix.environ[var]+"\""
>     print command
>     eval(command)
> -------------------------------
> > ./importer.py
> Traceback (innermost last):
>   File "./importer.py", line 12, in ?
>     eval(command)
>   File "<string>", line 1
>     WF_VC_PLUG_INS = "/usr/local/aw/userconfig/composer/plug-ins"
>                    ^
> SyntaxError: invalid syntax

> What am I doing wrong here?  How can I, at runtime, define a variable
> and assign it a value?

You need to use exec(), not eval(). This is probably a FAQ, but i'm not sure
if it's in the FAQ. From the Python Documentation however:

eval (expression[, globals[, locals]]) 
	The arguments are a string and two optional dictionaries. The
	expression argument is parsed and evaluated as a Python expression
	(technically speaking, a condition list) using the globals and
	locals dictionaries as global and local name space. If the locals
	dictionary is omitted it defaults to the globals dictionary. If both
	dictionaries are omitted, the expression is executed in the
	environment where eval is called.  The return value is the result of
	the evaluated expression. Syntax errors are reported as exceptions.

Note the use of the word 'expression' instead of 'code' or 'statement'.
Also, below the example:

Hints: dynamic execution of statements is supported by the exec statement. 

Below eval() is the documentation on execfile, with more hints. Curiously enough, the
documentation for the exec statement seems to be missing... Anyone know if
that's intentional or oversight ?

Anyway, for completeness:

>>> list = {'knight':'ni', 'answer':'42', 'range':'african or european'}
>>> for var in list.keys():
...     command = var + ' = "' + list[var] + '"'
...     print command
...     eval(command)
... 
range = "african or european"
Traceback (innermost last):
  File "<stdin>", line 4, in ?
  File "<string>", line 1
    range = "african or european"
          ^
SyntaxError: invalid syntax
>>> for var in list.keys():
...     command = var + ' = "' + list[var] + '"'
...     print command
...     exec(command)
... 
range = "african or european"
knight = "ni"
answer = "42"

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list