def importAny(name): try: return __import__(name,fromlist=['']) except: try: i = name.rfind('.') mod = __import__(name[:i],fromlist=['']) return getattr(mod,name[i+1:]) except: raise RuntimeError('No module of: %s found'%(name)) xxx = importAny('my.xxx') cls = importAny('my.xxx.SomeClass') #相当于 Class.forName('my.xxx.SomeClass') obj = cls('xxxx') #相当于newInstance(‘xxx')