{"record":{"id":"b66815e510f88e4e","repo":"pytest-dev/pytest","slug":"could-not-resolve-for-path","errorCode":null,"errorMessage":"Could not resolve for {path}","messagePattern":"Could not resolve for (.+?)","errorType":"exception","errorClass":"CouldNotResolvePathError","httpStatus":null,"severity":"error","filePath":"src/_pytest/pathlib.py","lineNumber":932,"sourceCode":"    pkg_root: Path | None = None\n    pkg_path = resolve_package_path(path)\n    if pkg_path is not None:\n        pkg_root = pkg_path.parent\n    if consider_namespace_packages:\n        start = pkg_root if pkg_root is not None else path.parent\n        for candidate in (start, *start.parents):\n            module_name = compute_module_name(candidate, path)\n            if module_name and is_importable(module_name, path):\n                # Point the pkg_root to the root of the namespace package.\n                pkg_root = candidate\n                break\n\n    if pkg_root is not None:\n        module_name = compute_module_name(pkg_root, path)\n        if module_name:\n            return pkg_root, module_name\n\n    raise CouldNotResolvePathError(f\"Could not resolve for {path}\")\n\n\ndef is_importable(module_name: str, module_path: Path) -> bool:\n    \"\"\"\n    Return if the given module path could be imported normally by Python, akin to the user\n    entering the REPL and importing the corresponding module name directly, and corresponds\n    to the module_path specified.\n\n    :param module_name:\n        Full module name that we want to check if is importable.\n        For example, \"app.models\".\n\n    :param module_path:\n        Full path to the python module/package we want to check if is importable.\n        For example, \"/projects/src/app/models.py\".\n    \"\"\"\n    try:\n        # Note this is different from what we do in ``_import_module_using_spec``, where we explicitly search through","sourceCodeStart":914,"sourceCodeEnd":950,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/pathlib.py#L914-L950","documentation":"Raised by resolve_pkg_root_and_module_name (lines 925-932) when no package root can be found for the given path. The function walks start and its parents, computing a module name and checking importability; if none of the candidates yields an importable module name, CouldNotResolvePathError (a subclass of ImportError) is raised. This is used by importlib import mode to map a file path to a canonical module name.","triggerScenarios":"A test file outside any importable package (no parent with __init__.py on sys.path); a path in a directory not on sys.path; namespace package resolution disabled and no regular package root found; consider_namespace_packages=False with a namespace layout.","commonSituations":"Running pytest from a directory not on sys.path; flat test files without package structure; broken sys.path in a custom runner; namespace packages when consideration is off.","solutions":["Ensure the file's package root is on sys.path (add a conftest.py or __init__.py).","Run pytest from the project root so the default rootdir insertion applies.","Enable namespace package consideration if applicable.","Add the directory via PYTHONPATH or a conftest at a higher level."],"exampleFix":"// before (file outside any importable package)\npytest tests/test_x.py  # tests has no __init__.py and is not on sys.path\n// after\ntouch tests/__init__.py   # or add a conftest.py at project root","handlingStrategy":"validation","validationCode":"try:\n    resolve_pkg_root_and_module_name(path)\nexcept CouldNotResolvePathError:\n    # add parent to sys.path or add __init__.py","typeGuard":"def _resolvable(path) -> bool:\n    try:\n        resolve_pkg_root_and_module_name(path)\n        return True\n    except CouldNotResolvePathError:\n        return False","tryCatchPattern":"try:\n    pkg_root, name = resolve_pkg_root_and_module_name(path)\nexcept CouldNotResolvePathError:\n    # fall back to path.parent / path.stem or fix sys.path\n    pkg_root, name = path.parent, path.stem","preventionTips":["Ensure the package root is on sys.path (add conftest.py / __init__.py).","Run pytest from the project root so default sys.path insertion applies."],"tags":["pytest","pathlib","importerror","collection","sys-path"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}