我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用pkgutil.ImpLoader()。
def _pl_find_on_path(self, fullname, path=None): subname = fullname.split(".")[-1] if self.path is None and subname != fullname: return None path = os.path.realpath(self.path) for (fp, ispkg) in FLS: for loader in self._loader_handlers: composed_path = fp % (('%s' + SEP + '%s') % (path, subname), loader.suffix) if os.path.isdir(composed_path): raise IOError("Invalid: Directory name ends in recognized suffix") if os.path.isfile(composed_path): return PolyLoader(fullname, composed_path, ispkg) # Fall back onto Python's own methods. try: file, filename, etc = imp.find_module(subname, [path]) except ImportError as e: return None return pkgutil.ImpLoader(fullname, file, filename, etc)
def find_module(self, fullname, path=None): # this duplicates most of ImpImporter.find_module subname = fullname.split(".")[-1] if subname != fullname and self.path is None: return None if self.path is None: path = None else: path = [os.path.realpath(self.path)] try: file, filename, etc = imp.find_module(subname, path) except ImportError: return None if file and etc[2] == imp.PY_SOURCE: outfilename = maybe_2to3(filename, modname=fullname) if outfilename != filename: file.close() filename = outfilename file = open(filename, 'rb') return ImpLoader(fullname, file, filename, etc) # setup the hook
def test_loader_deprecated(self): with self.check_deprecated(): x = pkgutil.ImpLoader("", "", "", "")
def find_module(self, fullname, paths=None): """ Returns a FavaLoader for fava files and a pkgutil.ImpLoader for standard python files. The ImpLoader is a PEP-302 wrapper for python's regular import machinery. """ name = fullname.split(".")[-1] filename = os.path.join(self.path, name + self.ext) if os.path.exists(filename): return FavaLoader(filename) return pkgutil.ImpLoader(fullname, *imp.find_module(name, [self.path]))