def find_loader(name, path=None):
"""Return the loader for the specified module.
This is a backward-compatible wrapper around find_spec().
This function is deprecated in favor of importlib.util.find_spec().
"""
warnings.warn('Use importlib.util.find_spec() instead.',
DeprecationWarning, stacklevel=2)
try:
loader = sys.modules[name].__loader__
if loader is None:
raise ValueError('{}.__loader__ is None'.format(name))
else:
return loader
except KeyError:
pass
except AttributeError:
six.raise_from(ValueError('{}.__loader__ is not set'.format(name)), None)
spec = _find_spec(name, path)
# We won't worry about malformed specs (missing attributes).
if spec is None:
return None
if spec.loader is None:
if spec.submodule_search_locations is None:
raise _ImportError('spec for {} missing loader'.format(name),
name=name)
raise _ImportError('namespace packages do not have loaders',
name=name)
return spec.loader
# This should be in all python >2.7
评论列表
文章目录