{"record":{"id":"474deb57a28bffa5","repo":"pytest-dev/pytest","slug":"type-self-cause-name-self-cause-from-s","errorCode":null,"errorMessage":"{type(self.cause).__name__}: {self.cause} (from {self.path})","messagePattern":"(.+?): (.+?) \\(from (.+?)\\)","errorType":"exception","errorClass":"ConftestImportFailure","httpStatus":null,"severity":"error","filePath":"src/_pytest/config/__init__.py","lineNumber":773,"sourceCode":"        # before loading the new one, otherwise the existing one will be\n        # returned from the module cache.\n        pkgpath = resolve_package_path(conftestpath)\n        if pkgpath is None:\n            try:\n                del sys.modules[conftestpath.stem]\n            except KeyError:\n                pass\n\n        try:\n            mod = import_path(\n                conftestpath,\n                mode=importmode,\n                root=rootpath,\n                consider_namespace_packages=consider_namespace_packages,\n            )\n        except Exception as e:\n            assert e.__traceback__ is not None\n            raise ConftestImportFailure(conftestpath, cause=e) from e\n\n        self._check_non_top_pytest_plugins(mod, conftestpath)\n\n        self._conftest_plugins.add(mod)\n        dirpath = conftestpath.parent\n        if dirpath in self._dirpath2confmods:\n            for path, mods in self._dirpath2confmods.items():\n                if dirpath in path.parents or path == dirpath:\n                    if mod in mods:\n                        raise AssertionError(\n                            f\"While trying to load conftest path {conftestpath!s}, \"\n                            f\"found that the module {mod} is already loaded with path {mod.__file__}. \"\n                            \"This is not supposed to happen. Please report this issue to pytest.\"\n                        )\n                    mods.append(mod)\n        self.trace(f\"loading conftestmodule {mod!r}\")\n        self.consider_conftest(mod, registration_name=conftestpath_plugin_name)\n        return mod","sourceCodeStart":755,"sourceCodeEnd":791,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/config/__init__.py#L755-L791","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\n# conftest.py\nimport missing_dep  # ImportError\n// after\n# conftest.py\ntry:\n    import missing_dep\nexcept ImportError:\n    missing_dep = None","handlingStrategy":"try-catch","validationCode":"import ast\nsrc = open('conftest.py').read()\nast.parse(src)  # catches syntax errors before pytest runs","typeGuard":"def conftest_imports_cleanly(path: str) -> bool:\n    import importlib.util\n    spec = importlib.util.spec_from_file_location('cf', path)\n    mod = importlib.util.module_from_spec(spec)\n    try:\n        spec.loader.exec_module(mod)\n        return True\n    except Exception:\n        return False","tryCatchPattern":"from _pytest.config import ConftestImportFailure\ntry:\n    pytest.main(['tests'])\nexcept ConftestImportFailure as e:\n    print(f'{e.path}: {type(e.cause).__name__}: {e.cause}')","preventionTips":["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"],"tags":["conftest","import","config","plugin"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}