{"id":"0d6a143f1156f599","repo":"pytest-dev/pytest","slug":"path","errorCode":null,"errorMessage":"{path}","messagePattern":"\\{path\\}","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"critical","filePath":"src/_pytest/pathlib.py","lineNumber":590,"sourceCode":"          allows having same-named test modules in different places.\n\n    :param root:\n        Used as an anchor when mode == ImportMode.importlib to obtain\n        a unique name for the module being imported so it can safely be stored\n        into ``sys.modules``.\n\n    :param consider_namespace_packages:\n        If True, consider namespace packages when resolving module names.\n\n    :raises ImportPathMismatchError:\n        If after importing the given `path` and the module `__file__`\n        are different. Only raised in `prepend` and `append` modes.\n    \"\"\"\n    path = Path(path)\n    mode = ImportMode(mode)\n\n    if not path.exists():\n        raise ImportError(path)\n\n    if mode is ImportMode.importlib:\n        # Try to import this module using the standard import mechanisms, but\n        # without touching sys.path.\n        try:\n            _, module_name = resolve_pkg_root_and_module_name(\n                path, consider_namespace_packages=consider_namespace_packages\n            )\n        except CouldNotResolvePathError:\n            pass\n        else:\n            # If the given module name is already in sys.modules, do not import it again.\n            with contextlib.suppress(KeyError):\n                return sys.modules[module_name]\n\n            mod = _import_module_using_spec(module_name, path, insert_modules=False)\n            if mod is not None:\n                return mod","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/pathlib.py#L572-L608","documentation":"import_path raises ImportError(path) when the given file path does not exist on disk. pytest uses import_path to load test modules and conftest.py files, so a non-existent path usually means a stale reference or a deleted/moved file.","triggerScenarios":"import_path called with a path that fails path.exists(); e.g. a conftest or test module path recorded by the rewriter/cache that has since been deleted, or a manually-constructed wrong path.","commonSituations":"Deleted or moved test files still referenced by an IDE/runner; symlinks pointing nowhere; misconfigured rootdir/python path; stale .pyc without source.","solutions":["Verify the file exists at the given path before importing","Check for typos, broken symlinks, or moved files","Regenerate the path list / clear caches pointing at the stale path"],"exampleFix":"// before\nmod = import_path('tests/old_test.py')\n// after\nfrom pathlib import Path\np = Path('tests/new_test.py')\nassert p.exists()\nmod = import_path(p)","handlingStrategy":"validation","validationCode":"from pathlib import Path\ndef import_if_exists(path):\n    p = Path(path)\n    if not p.exists():\n        raise FileNotFoundError(p)\n    from _pytest.pathlib import import_path\n    return import_path(p)","typeGuard":"from pathlib import Path\ndef path_exists(path) -> bool:\n    return Path(path).exists()","tryCatchPattern":null,"preventionTips":["Check path.exists() before calling import_path","Regenerate path lists after renaming or moving test files"],"tags":["pathlib","import","filesystem","pytest"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}