how to get the environment variable??

Ben Hutchings ben.hutchings at
Thu May 10 20:08:28 EDT 2001

ed_tsang at writes:

> hi i am trying to set a global variable called tharn from the 
> environemtn variable in my .cshrc file ... 

You mean, in your program's environment.

> I used the folowing code:
> try:
>     tharn = os.environ['THARN']
> except KeyError:
>     try:
>        tharn = os.environ['CTF']
>     except KeyError:
>        print "\nEnvironment variable THARN must be set"
>        print "before starting the Test Harness."

Change the message to match the code ('variable THARN or CTF')!

>        sys.exit(1)
> The problem I am facing is tharn can be set by either environment 
> varible CTF or THARN. Only one of CTF and THARN should  exist, or 
> they can be both missing then user get a wanring message...

What if they're both set?

> I amnot sure if my code is archieveing that....
> or any potential pittfall in this ... wondering if I may intoduce 
> error in having another try .. except block in KeyError...

You certainly can have nested try...except blocks.

Any opinions expressed are my own and not necessarily those of Roundpoint.

More information about the Python-list mailing list