{"record":{"id":"0d6a143f1156f599","repo":"pytest-dev/pytest","slug":"path","errorCode":null,"errorMessage":"{path}","messagePattern":"\\{path\\}","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","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/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/pathlib.py#L572-L608","documentation":"Raised by import_path (line 590) when the given path does not exist on disk (path.exists() is False). This is the first guard before any import-mode logic; a non-existent .py file cannot be imported, so ImportError(path) is raised. Used during collection when pytest resolves a test file path.","triggerScenarios":"Collecting a test file that was deleted or moved after the session started; symbolic link pointing to a missing target; typo in a conftest or test path passed to pytest; py.path vs pathlib.Path mismatch yielding an empty path.","commonSituations":"git operations during a run that remove files; editor saves that truncate to empty then rewrite; broken symlinks in the test tree; --pyargs pointing at a non-installable module.","solutions":["Verify the file exists: pathlib.Path(path).exists() before running.","Restore the deleted/moved file or fix the path argument.","Remove stale symlinks or broken references from the collection root."],"exampleFix":"// before\npytest tests/test_missing.py\n// after\npytest tests/test_existing.py","handlingStrategy":"validation","validationCode":"import pathlib\nif not pathlib.Path(path).exists():\n    raise FileNotFoundError(path)","typeGuard":"def _path_exists(p) -> bool:\n    import pathlib\n    return pathlib.Path(p).exists()","tryCatchPattern":null,"preventionTips":["Verify test paths exist before invoking pytest.","Avoid deleting/moving test files mid-session."],"tags":["pytest","pathlib","importerror","filesystem","collection"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}