pytest-dev/pytest · error · ImportError
Error importing plugin "{modname}": {e.args[0]}
Error message
Error importing plugin "{modname}": {e.args[0]} What it means
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.
Source
Thrown at src/_pytest/config/__init__.py:927
importspec = "_pytest." + modname if modname in builtin_plugins else modname
self.rewrite_hook.mark_rewrite(importspec)
if consider_entry_points:
loaded = self.load_setuptools_entrypoints("pytest11", name=modname)
if loaded:
return
try:
if sys.version_info >= (3, 11):
mod = importlib.import_module(importspec)
else:
# On Python 3.10, import_module breaks
# testing/test_config.py::test_disable_plugin_autoload.
__import__(importspec)
mod = sys.modules[importspec]
except ImportError as e:
raise ImportError(
f'Error importing plugin "{modname}": {e.args[0]}'
).with_traceback(e.__traceback__) from e
except Skipped as e:
self.skipped_plugins.append((modname, e.msg or ""))
else:
self.register(mod, modname)
def _get_plugin_specs_as_list(
specs: types.ModuleType | str | Sequence[str] | None,
) -> list[str]:
"""Parse a plugins specification into a list of plugin names."""
# None means empty.
if specs is None:
return []
# Workaround for #3899 - a submodule which happens to be called "pytest_plugins".
if isinstance(specs, types.ModuleType):View on GitHub (pinned to 98b357f69e)
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.
Example fix
# before (pyproject.toml) [tool.pytest.ini_options] addopts = '-p pytest_cov' # but pytest-cov not installed # after pip install pytest-cov
Defensive patterns
Strategy: try-catch
Validate before calling
import importlib.util
def can_import_plugin(modname: str) -> bool:
"""Check if a plugin module is importable without side effects."""
spec = importlib.util.find_spec(modname)
return spec is not None Try / catch
import pytest
try:
exit_code = pytest.main(['-p', 'my_plugin', 'tests/'])
except ImportError as e:
if 'Error importing plugin' in str(e):
print(f'Plugin import failed: {e}')
# install or remove the plugin reference
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Missing required plugins: {}
- plugin {name} cannot be disabled
- Blocking conftest files using -p is not supported: -p no:{na
- Plugins may be specified as a sequence or a ','-separated st
- Specifying plugins as objects is not supported in pytester s
AI-assisted analysis of pytest-dev/pytest@98b357f69e (2026-08-04).
Data as JSON: /data/errors/c2a05d2c9c908639.json.
Report an issue: GitHub.