Just spent the morning learning all about sys.modules (out of necessity, I assure you :). I have a module named "endue.bizobjects.bizobjectkit" with a skeleton similar to:

\nallBizObjectServers = {}\n\nclass BizObject:\n    \n    def __init__(self):\n        pass\n\nclass BizComponent(BizObject):\n    relation = None\n\n    def __init__(self):\n        self.relation = BizRelation()\n\n    def childObject(self):\n        return self.relation.child()\n\nclass BizRelation(BizObject):\n    childComponent = None\n\n    def __init__(self):\n        pass\n\n    def child(self):\n        raise StandardError(allBizObjectServers)\n        return self.childComponent


Only problem was, when BizComponent.childObject() called BizRelation.child(), my allBizObjectServers dictionary was suddenly empty (hence the raise StandardError call, which I put in during debugging to inform me of that fact).

The issue turned out to be that I actually had two copies of endue.bizobjects.bizobjectkit loaded at once; dumping sys.modules.keys() showed both 'endue.bizobjects.bizobjectkit' and 'bizobjectkit'. Turned out I was dynamically importing some classes (like BizRelation) from bizobjectkit, and my import routine was loading a second copy of the bizobjectkit module with its own (blank) allBizObjectServers dictionary. Bleah.

The fix was testing to see if the dynamic classes I wanted were already present in the current module (get_class() is my dynamic class loader):

try:\n    aClass = getattr(sys.modules[__name__], aClassName)\nexcept AttributeError:\n    aClass = get_class(fullClassName)