#This catches the exception raised when importing buggy and then traverses #the backtrace hunting for the relevant binding. Whenever it finds it, #it prints the source scope and value. Running this reveals the problem: #Even though whatKnightsSay is the string 'Ni!', this kind of inspection #finds the value "None" in two different scopes. import sys variableName = 'whatKnightsSay' try: import buggy except: t, v, tb = sys.exc_info() print 'Exception:', `t, v` print 'Searching stack for reference to %s:' % (variableName,) while tb != None: f = tb.tb_frame c = f.f_code print 'At:', for attrName in ['filename', 'name', 'varnames']: print '%s: %s, ' % (attrName, getattr(c, 'co_' + attrName)), print for scopeName in ['globals', 'locals']: scopeDict = getattr(f, 'f_' + scopeName) if variableName in scopeDict: print '%s reference: %s = %r' % (scopeName, variableName, scopeDict[variableName]) tb = tb.tb_next