Weird import behavior

Tommytrojan hauck at gseos.com
Tue Oct 25 13:11:22 EDT 2005


Hi,

I have been using Python for a while but today I came across a really
strange behavior:

While poking around in Queue.py due to problems with importing this
module from a thread I got an error that a module that I imported on
top of the file could not be accessed. I reduced the problem to this
small script:

import string
import os

class T:
    def __init__(self):
        try:
            print 'xxx nothing to do'
        except ImportError:
            print 'got an import error'
            import os as string

        print 'xxxx string module', string

t=T()


The import clause in the except statement (although never executed)
removes the reference to the string module imported at the beginning of
the script. Any idea why this is the case? 

Thanks,
  Tommy




More information about the Python-list mailing list