MCPcopy Create free account
hub / github.com/pytest-dev/pytest / import_plugin

Method import_plugin

src/_pytest/config/__init__.py:718–753  ·  view source on GitHub ↗

Import a plugin with ``modname``. If ``consider_entry_points`` is True, entry point names are also considered to find a plugin.

(self, modname: str, consider_entry_points: bool = False)

Source from the content-addressed store, hash-verified

716 self.import_plugin(import_spec)
717
718 def import_plugin(self, modname: str, consider_entry_points: bool = False) -> None:
719 """Import a plugin with ``modname``.
720
721 If ``consider_entry_points`` is True, entry point names are also
722 considered to find a plugin.
723 """
724 # Most often modname refers to builtin modules, e.g. "pytester",
725 # "terminal" or "capture". Those plugins are registered under their
726 # basename for historic purposes but must be imported with the
727 # _pytest prefix.
728 assert isinstance(modname, str), (
729 "module name as text required, got %r" % modname
730 )
731 if self.is_blocked(modname) or self.get_plugin(modname) is not None:
732 return
733
734 importspec = "_pytest." + modname if modname in builtin_plugins else modname
735 self.rewrite_hook.mark_rewrite(importspec)
736
737 if consider_entry_points:
738 loaded = self.load_setuptools_entrypoints("pytest11", name=modname)
739 if loaded:
740 return
741
742 try:
743 __import__(importspec)
744 except ImportError as e:
745 raise ImportError(
746 f'Error importing plugin "{modname}": {e.args[0]}'
747 ).with_traceback(e.__traceback__) from e
748
749 except Skipped as e:
750 self.skipped_plugins.append((modname, e.msg or ""))
751 else:
752 mod = sys.modules[importspec]
753 self.register(mod, modname)
754
755
756def _get_plugin_specs_as_list(

Calls 3

registerMethod · 0.95
appendMethod · 0.80
mark_rewriteMethod · 0.45