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)
| 885 | self.import_plugin(import_spec) |
| 886 | |
| 887 | def import_plugin(self, modname: str, consider_entry_points: bool = False) -> None: |
| 888 | """Import a plugin with ``modname``. |
| 889 | |
| 890 | If ``consider_entry_points`` is True, entry point names are also |
| 891 | considered to find a plugin. |
| 892 | """ |
| 893 | # Most often modname refers to builtin modules, e.g. "pytester", |
| 894 | # "terminal" or "capture". Those plugins are registered under their |
| 895 | # basename for historic purposes but must be imported with the |
| 896 | # _pytest prefix. |
| 897 | assert isinstance(modname, str), ( |
| 898 | f"module name as text required, got {modname!r}" |
| 899 | ) |
| 900 | if self.is_blocked(modname) or self.get_plugin(modname) is not None: |
| 901 | return |
| 902 | |
| 903 | importspec = "_pytest." + modname if modname in builtin_plugins else modname |
| 904 | self.rewrite_hook.mark_rewrite(importspec) |
| 905 | |
| 906 | if consider_entry_points: |
| 907 | loaded = self.load_setuptools_entrypoints("pytest11", name=modname) |
| 908 | if loaded: |
| 909 | return |
| 910 | |
| 911 | try: |
| 912 | if sys.version_info >= (3, 11): |
| 913 | mod = importlib.import_module(importspec) |
| 914 | else: |
| 915 | # On Python 3.10, import_module breaks |
| 916 | # testing/test_config.py::test_disable_plugin_autoload. |
| 917 | __import__(importspec) |
| 918 | mod = sys.modules[importspec] |
| 919 | except ImportError as e: |
| 920 | raise ImportError( |
| 921 | f'Error importing plugin "{modname}": {e.args[0]}' |
| 922 | ).with_traceback(e.__traceback__) from e |
| 923 | |
| 924 | except Skipped as e: |
| 925 | self.skipped_plugins.append((modname, e.msg or "")) |
| 926 | else: |
| 927 | self.register(mod, modname) |
| 928 | |
| 929 | |
| 930 | def _get_plugin_specs_as_list( |