{"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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/pathlib.py#L914-L950","documentation":"Thrown by resolve_pkg_root_and_module_name when a Python file path cannot be mapped to a package root because no ancestor directory contains an __init__.py (and, with namespace packages enabled, no importable namespace root is found). The function walks parents looking for the last directory holding __init__.py and computes the dotted module name; if both the regular and namespace-package searches fail it raises CouldNotResolvePathError. It typically surfaces during collection/import when pytest needs the module's importable name for the test's node id.","triggerScenarios":"resolve_pkg_root_and_module_name(path) is called on a .py file whose entire parent chain has no __init__.py files (a standalone script outside any package), or whose candidate root directory name is not a valid Python identifier, or where the computed module name does not correspond to an importable path on sys.path.","commonSituations":"Running pytest against a loose test file with no __init__.py in its directory; tests placed in a project that uses a non-src layout where the test dir is not a package; renaming a package directory to something containing hyphens or starting with a digit; enabling consider_namespace_packages while sys.path does not include the namespace root.","solutions":["Add an empty __init__.py to the test file's directory (and ancestor dirs) so it forms a proper package.","Ensure the directory names from the package root down to the file are valid Python identifiers (no hyphens, no leading digits).","If you intend namespace packages, make sure the root directory is on sys.path (e.g. via pythonpath ini option or conftest.py) and enable namespace package consideration.","Move the test file into the existing package tree (e.g. under src/pkgname/tests/) so it inherits an importable package root."],"exampleFix":"// before (structure)\nproject/\n  tests/\n    test_foo.py          # no __init__.py anywhere\n// after\nproject/\n  tests/\n    __init__.py\n    test_foo.py","handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ndef has_package_root(path: Path) -> bool:\n    return any((p / \"__init__.py\").is_file() and p.name.isidentifier()\n               for p in [path.parent, *path.parents])\n\n# before calling resolve_pkg_root_and_module_name\nif not has_package_root(Path(mypath)):\n    raise SystemExit(f\"{mypath} is not inside a Python package; add __init__.py\")","typeGuard":null,"tryCatchPattern":"from _pytest.pathlib import CouldNotResolvePathError\ntry:\n    pkg_root, modname = resolve_pkg_root_and_module_name(p)\nexcept CouldNotResolvePathError:\n    # fall back to treating as a standalone module or surface a clear error\n    ...","preventionTips":["Always create __init__.py in test and source directories.","Keep package directory names valid Python identifiers (no hyphens/digits-leading).","Use a src/ layout so package roots are unambiguous.","Run 'python -c \"import mypackage\"' to confirm importability before running pytest."],"tags":["python","path-resolution","packaging","collection"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}