why os.environ dictionary remembers previously set variables??

Donn Cave donn at u.washington.edu
Fri May 11 16:06:26 EDT 2001


If you execute your python program from a csh script, that script
will invoke .cshrc, and that would account for the behavior you
describe.  In most cases, shell scripts are better written for the
Bourne shell (sh).  To make sure they actually run that way, make
the script executable (chmod +x) and put #!/bin/sh right at the top.
If you're not sure whether .cshrc is being executed again, put an
echo command in it.

	Donn Cave, donn at u.washington.edu
----------------------------------------
Quoth ed_tsang at yahoo.com:
| Hi I use the code 
| print os.environ['THARN']
| print os.environ['CTF']
| if os.environ.has_key('THARN') and os.environ.has_key('CTF'):
|     print "\nEither THARN or CTF shall be set not BOTH"
|     print "before starting the Test Harness."
|     sys.exit(1)
| elif os.environ.has_key('THARN'):
|     tharn = os.environ['THARN']
|     print 'THARN'
| elif os.environ.has_key('CTF'):
|     tharn = os.environ['THARN']
|     print 'CTF'
| else: 
|     print "\nEnvironment variable THARN must be set"
|     print "before starting the Test Harness."
|     sys.exit(1)
| to do wset a local global thanr variable. 
|
| In the .cshrc file in my home directory , I had:
| setenv THARN /vobs/qa/stf/ctf/src/ctf 
| set in the first run of my script.
| It prints the correct content of tharn.
| Then I exit the process, unset tne variable using
| unsetenv THARN
| setenv CTF /vobs/qa/stf/ctf/src/ctf
| and uses echo to make sure THARN is gone while CTF is set.
| Then I run the script again.. t my dismay, it prints both CTF and 
| THARN having the value:
|
| /vobs/qa/stf/ctf/src/ctf
| /vobs/qa/stf/ctf/src/ctf
|
| Either THARN or CTF shall be set not BOTH
| before starting the Test Harness.
|  I then go to delete the .pyc file and run it again same thing 
| happens!!!
| I use echo again and the system shows THRAN is gone while CTF is here 
| which is expected... then why the heck python remembers the the alst 
| one and just append the new environment variable???
|
| What to do?? should I expliciltly delete all the variables in 
| os.environ dict before exiting.. As you know it depends on user to 
| set CTF ,THRAN , BOTH (error) or none at all (error). The latter use 
| are error cases.
|
| thanks



More information about the Python-list mailing list