def importModule(modulePath, name=""):
if isDottedPath(modulePath) or not os.path.exists(modulePath):
try:
return __import__(modulePath, fromlist="dummy")
except ImportError:
logger.debug("failed to load module->%s" % modulePath, exc_info=True)
try:
if os.path.exists(modulePath):
if not name:
name = os.path.splitext(os.path.basename(modulePath))[0]
if name in sys.modules:
return sys.modules[name]
if os.path.isdir(modulePath):
modulePath = os.path.join(modulePath, "__init__.py")
if not os.path.exists(modulePath):
raise ValueError("Cannot find modulepath: {}".format(modulePath))
return imp.load_source(name, os.path.realpath(modulePath))
except ImportError:
logger.error("Failed to load module {}".format(modulePath))
raise
评论列表
文章目录