Changing environment variables into python variables

sragsdale at my-deja.com sragsdale at my-deja.com
Thu Jun 3 22:52:16 EDT 1999


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:

foreach my $var (keys(%ENV)) {
  my $cmd = "\$".$var." = '".$ENV{$var}."'";
  print $cmd,"\n"; # just to see what I'm doing
  eval($cmd);
}

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?


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list