pytest-dev/pytest · error · ConftestImportFailure
{type(self.cause).__name__}: {self.cause} (from {self.path})
Error message
{type(self.cause).__name__}: {self.cause} (from {self.path}) What it means
Raised by _try_load_conftest() at config/__init__.py:773 when import_path() fails while loading a conftest.py. Any exception during import is wrapped in ConftestImportFailure (defined at config/__init__.py:130) which preserves path and cause; __str__ at config/__init__.py:140-141 renders it as '<CauseType>: <cause msg> (from <path>)'. pytest surfaces this in its internal-error report and filters the traceback via filter_traceback_for_conftest_import_failure() (config/__init__.py:144).
Source
Thrown at src/_pytest/config/__init__.py:773
# before loading the new one, otherwise the existing one will be
# returned from the module cache.
pkgpath = resolve_package_path(conftestpath)
if pkgpath is None:
try:
del sys.modules[conftestpath.stem]
except KeyError:
pass
try:
mod = import_path(
conftestpath,
mode=importmode,
root=rootpath,
consider_namespace_packages=consider_namespace_packages,
)
except Exception as e:
assert e.__traceback__ is not None
raise ConftestImportFailure(conftestpath, cause=e) from e
self._check_non_top_pytest_plugins(mod, conftestpath)
self._conftest_plugins.add(mod)
dirpath = conftestpath.parent
if dirpath in self._dirpath2confmods:
for path, mods in self._dirpath2confmods.items():
if dirpath in path.parents or path == dirpath:
if mod in mods:
raise AssertionError(
f"While trying to load conftest path {conftestpath!s}, "
f"found that the module {mod} is already loaded with path {mod.__file__}. "
"This is not supposed to happen. Please report this issue to pytest."
)
mods.append(mod)
self.trace(f"loading conftestmodule {mod!r}")
self.consider_conftest(mod, registration_name=conftestpath_plugin_name)
return modView on GitHub (pinned to 0d6fbdeffa)
Solutions
- Read the wrapped cause in the message - the original exception type and text are preserved verbatim
- Open the conftest.py at the reported path and fix the import/syntax error
- Run `python -c "import <module>"` against the offending conftest to reproduce the error outside pytest
- Check that any conftest-level plugin listed in pytest_plugins is pip-installed in the active environment
Example fix
// before
# conftest.py
import missing_dep # ImportError
// after
# conftest.py
try:
import missing_dep
except ImportError:
missing_dep = None Defensive patterns
Strategy: try-catch
Validate before calling
import ast
src = open('conftest.py').read()
ast.parse(src) # catches syntax errors before pytest runs Type guard
def conftest_imports_cleanly(path: str) -> bool:
import importlib.util
spec = importlib.util.spec_from_file_location('cf', path)
mod = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(mod)
return True
except Exception:
return False Try / catch
from _pytest.config import ConftestImportFailure
try:
pytest.main(['tests'])
except ConftestImportFailure as e:
print(f'{e.path}: {type(e.cause).__name__}: {e.cause}') Prevention
- Pre-validate conftest.py by importing it in isolation: python -c 'import conftest'
- Keep conftest.py imports minimal and dependency-light
- Use separate venv checks in CI to ensure all conftest dependencies are installed
When it happens
Trigger: A conftest.py at any level (rootdir, tests/, subdir) contains a SyntaxError, ImportError (missing dependency), NameError, or any exception at module top-level. capture.py:764-770 calls import_path(); the bare `except Exception as e` at capture.py:771 wraps it.
Common situations: Adding a new conftest.py that imports a not-yet-installed dependency; upgrading pytest plugins that change APIs; circular imports between conftest.py files; python path issues when rootdir is mis-detected.
Related errors
- Blocking conftest files using -p is not supported: -p no:{na
- Error importing plugin "{modname}": {e.args[0]}
- Plugins may be specified as a sequence or a ','-separated st
- plugin {name} cannot be disabled
- Missing required plugins: {}
AI-assisted analysis of pytest-dev/pytest@0d6fbdeffa (2026-08-11).
Data as JSON: /api/errors/474deb57a28bffa5.
Report an issue: GitHub.