{"id":"c2a05d2c9c908639","repo":"pytest-dev/pytest","slug":"error-importing-plugin-modname-e-args-0","errorCode":null,"errorMessage":"Error importing plugin \"{modname}\": {e.args[0]}","messagePattern":"Error importing plugin \"(.+?)\": (.+?)","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"src/_pytest/config/__init__.py","lineNumber":927,"sourceCode":"\n        importspec = \"_pytest.\" + modname if modname in builtin_plugins else modname\n        self.rewrite_hook.mark_rewrite(importspec)\n\n        if consider_entry_points:\n            loaded = self.load_setuptools_entrypoints(\"pytest11\", name=modname)\n            if loaded:\n                return\n\n        try:\n            if sys.version_info >= (3, 11):\n                mod = importlib.import_module(importspec)\n            else:\n                # On Python 3.10, import_module breaks\n                # testing/test_config.py::test_disable_plugin_autoload.\n                __import__(importspec)\n                mod = sys.modules[importspec]\n        except ImportError as e:\n            raise ImportError(\n                f'Error importing plugin \"{modname}\": {e.args[0]}'\n            ).with_traceback(e.__traceback__) from e\n\n        except Skipped as e:\n            self.skipped_plugins.append((modname, e.msg or \"\"))\n        else:\n            self.register(mod, modname)\n\n\ndef _get_plugin_specs_as_list(\n    specs: types.ModuleType | str | Sequence[str] | None,\n) -> list[str]:\n    \"\"\"Parse a plugins specification into a list of plugin names.\"\"\"\n    # None means empty.\n    if specs is None:\n        return []\n    # Workaround for #3899 - a submodule which happens to be called \"pytest_plugins\".\n    if isinstance(specs, types.ModuleType):","sourceCodeStart":909,"sourceCodeEnd":945,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/config/__init__.py#L909-L945","documentation":"When pytest attempts to import a plugin module (via -p NAME, PYTEST_PLUGINS, pytest_plugins in conftest, or entry points) and the import fails with ImportError, pytest re-raises a wrapped ImportError that clearly names the offending plugin. This surfaces broken installs, missing dependencies, or syntax errors in plugin code during startup.","triggerScenarios":"Specifying a plugin that is not installed (e.g., -p pytest_foo when pytest-foo is absent), a plugin with an unmet dependency, or a plugin module with a syntax/import error. importlib.import_module(importspec) raises ImportError and the message is wrapped.","commonSituations":"Missing pip install for a third-party plugin referenced in pyproject.toml's addopts, a conftest.py listing a plugin in pytest_plugins that isn't installed in the current venv, or a plugin broken by a Python version upgrade.","solutions":["Install the missing plugin: pip install <plugin_name>.","Check the original ImportError message (e.args[0]) for the root cause (missing module, syntax error, etc.).","Remove the plugin from PYTEST_PLUGINS, addopts, or pytest_plugins if it is no longer needed.","Activate the correct virtualenv where the plugin is installed."],"exampleFix":"# before (pyproject.toml)\n[tool.pytest.ini_options]\naddopts = '-p pytest_cov'\n# but pytest-cov not installed\n\n# after\npip install pytest-cov","handlingStrategy":"try-catch","validationCode":"import importlib.util\n\ndef can_import_plugin(modname: str) -> bool:\n    \"\"\"Check if a plugin module is importable without side effects.\"\"\"\n    spec = importlib.util.find_spec(modname)\n    return spec is not None","typeGuard":null,"tryCatchPattern":"import pytest\n\ntry:\n    exit_code = pytest.main(['-p', 'my_plugin', 'tests/'])\nexcept ImportError as e:\n    if 'Error importing plugin' in str(e):\n        print(f'Plugin import failed: {e}')\n        # install or remove the plugin reference\n    raise","preventionTips":["Pin all plugin dependencies in requirements.txt or pyproject.toml with versions.","Use a dedicated test requirements file installed in CI and local venv.","Run pip install -e '.[dev]' or equivalent before pytest to ensure plugins are present."],"tags":["plugins","import-error","dependency","startup"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}