我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用importlib.machinery.SourceFileLoader()。
def import_arbitrary(fileName, modName): """ Returns a module reference. This function acts like a Python import statement, but it will import an arbitrary Python file located at any path. """ importlib.invalidate_caches() loader = ilmac.SourceFileLoader(modName.replace('.', '_'), fileName) spec = ilutil.spec_from_loader(loader.name, loader) mod = ilutil.module_from_spec(spec) loader.exec_module(mod) return mod # End import_arbitrary
def load_source(name, pathname, file=None): loader = _LoadSourceCompatibility(name, pathname, file) spec = util.spec_from_file_location(name, pathname, loader=loader) if name in sys.modules: module = _exec(spec, sys.modules[name]) else: module = _load(spec) # To allow reloading to potentially work, use a non-hacked loader which # won't rely on a now-closed file object. module.__loader__ = machinery.SourceFileLoader(name, pathname) module.__spec__.loader = module.__loader__ return module