def lookup_by_objref(objref):
"""
Imports an object by an ObjRef object.
If ObjRef object also contains module attribute, it will also attempt to relative import from it
when absolute import was not successful.
"""
obj = pydoc.locate(objref.name)
if obj is None:
if objref.module is None:
raise ImportError('Unable to import "%s"' % (objref.name))
path = '.'.join([objref.module, objref.name])
obj = pydoc.locate(path)
if obj is None:
raise ImportError('Unable to import "%s" nor "%s"' % (objref.name, path))
return obj
评论列表
文章目录