MCPcopy Index your code
hub / github.com/python/cpython / ModuleSpec

Class ModuleSpec

Lib/importlib/_bootstrap.py:547–643  ·  view source on GitHub ↗

The specification for a module, used for loading. A module's spec is the source for information about the module. For data associated with the module, including source, use the spec's loader. `name` is the absolute name of the module. `loader` is the loader to use when loadin

Source from the content-addressed store, hash-verified

545
546
547class ModuleSpec:
548 """The specification for a module, used for loading.
549
550 A module's spec is the source for information about the module. For
551 data associated with the module, including source, use the spec's
552 loader.
553
554 `name` is the absolute name of the module. `loader` is the loader
555 to use when loading the module. `parent` is the name of the
556 package the module is in. The parent is derived from the name.
557
558 `is_package` determines if the module is considered a package or
559 not. On modules this is reflected by the `__path__` attribute.
560
561 `origin` is the specific location used by the loader from which to
562 load the module, if that information is available. When filename is
563 set, origin will match.
564
565 `has_location` indicates that a spec's "origin" reflects a location.
566 When this is True, `__file__` attribute of the module is set.
567
568 `cached` is the location of the cached bytecode file, if any.
569
570 `submodule_search_locations` is the sequence of path entries to
571 search when importing submodules. If set, is_package should be
572 True--and False otherwise.
573
574 Packages are simply modules that (may) have submodules. If a spec
575 has a non-None value in `submodule_search_locations`, the import
576 system will consider modules loaded from the spec as packages.
577
578 Only finders (see importlib.abc.MetaPathFinder and
579 importlib.abc.PathEntryFinder) should modify ModuleSpec instances.
580
581 """
582
583 def __init__(self, name, loader, *, origin=None, loader_state=None,
584 is_package=None):
585 self.name = name
586 self.loader = loader
587 self.origin = origin
588 self.loader_state = loader_state
589 self.submodule_search_locations = [] if is_package else None
590 self._uninitialized_submodules = []
591
592 # file-location attributes
593 self._set_fileattr = False
594 self._cached = None
595
596 def __repr__(self):
597 args = [f'name={self.name!r}', f'loader={self.loader!r}']
598 if self.origin is not None:
599 args.append(f'origin={self.origin!r}')
600 if self.submodule_search_locations is not None:
601 args.append(f'submodule_search_locations={self.submodule_search_locations}')
602 return f'{self.__class__.__name__}({", ".join(args)})'
603
604 def __eq__(self, other):

Callers 4

test_loaderMethod · 0.85
spec_from_loaderFunction · 0.85
_spec_from_moduleFunction · 0.85

Calls

no outgoing calls

Tested by 1

test_loaderMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…