Skip to content

Instantly share code, notes, and snippets.

@xianghuzhao
Last active November 2, 2016 10:04
Show Gist options
  • Save xianghuzhao/84a477f4f71085119081c25eb01e8259 to your computer and use it in GitHub Desktop.
Save xianghuzhao/84a477f4f71085119081c25eb01e8259 to your computer and use it in GitHub Desktop.
Load Object
def loadObject(moduleName, className):
try:
m = __import__(moduleName, globals(), locals(), [className])
except ImportError, e:
raise Exception('Could not from %s import %s' % (moduleName, className))
return getattr(m, className)()
import imp
def loadObject2(moduleName, className):
pathname = None
for n in moduleName.split('.'):
path = None if (pathname is None) else [pathname]
try:
fp, pathname, description = imp.find_module(n, path)
except ImportError, e:
raise ImportError('Could not from %s import %s' % (moduleName, className))
try:
m = imp.load_module(className, fp, pathname, description)
finally:
if fp:
fp.close()
return getattr(m, className)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment