{"id":"161155864cfaeb01","repo":"pypa/pip","slug":"cannot-import-mod-path-r","errorCode":null,"errorMessage":"Cannot import {mod_path!r}","messagePattern":"Cannot import (.+?)","errorType":"exception","errorClass":"BackendUnavailable","httpStatus":null,"severity":"critical","filePath":"src/pip/_vendor/pyproject_hooks/_in_process/_in_process.py","lineNumber":73,"sourceCode":"        self.hook_name = hook_name\n\n\ndef _build_backend():\n    \"\"\"Find and load the build backend\"\"\"\n    backend_path = os.environ.get(\"_PYPROJECT_HOOKS_BACKEND_PATH\")\n    ep = os.environ[\"_PYPROJECT_HOOKS_BUILD_BACKEND\"]\n    mod_path, _, obj_path = ep.partition(\":\")\n\n    if backend_path:\n        # Ensure in-tree backend directories have the highest priority when importing.\n        extra_pathitems = backend_path.split(os.pathsep)\n        sys.meta_path.insert(0, _BackendPathFinder(extra_pathitems, mod_path))\n\n    try:\n        obj = import_module(mod_path)\n    except ImportError:\n        msg = f\"Cannot import {mod_path!r}\"\n        raise BackendUnavailable(msg, traceback.format_exc())\n\n    if obj_path:\n        for path_part in obj_path.split(\".\"):\n            obj = getattr(obj, path_part)\n    return obj\n\n\nclass _BackendPathFinder:\n    \"\"\"Implements the MetaPathFinder interface to locate modules in ``backend-path``.\n\n    Since the environment provided by the frontend can contain all sorts of\n    MetaPathFinders, the only way to ensure the backend is loaded from the\n    right place is to prepend our own.\n    \"\"\"\n\n    def __init__(self, backend_path, backend_module):\n        self.backend_path = backend_path\n        self.backend_module = backend_module","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/pyproject_hooks/_in_process/_in_process.py#L55-L91","documentation":"Inside the in-process subprocess, _build_backend() raises BackendUnavailable('Cannot import {mod_path!r}') when import_module(mod_path) fails with ImportError and no backend-path finder is in play. mod_path is the module portion of the build-backend spec (before any ':'). The full traceback is captured and propagated back to the parent process.","triggerScenarios":"The build-backend module name (e.g. 'flit_core.buildapi') cannot be imported in the build environment: the package is absent, or one of its imports fails. This is the subprocess-side cause that surfaces in the parent as BackendUnavailable.","commonSituations":"Backend package not in build-system.requires; backend installed but a transitive dependency missing; Python version mismatch breaking an import; a typo in the module portion of build-backend.","solutions":["Add the backend package to [build-system] requires in pyproject.toml.","Correct any typo in the build-backend module path.","Install the backend and its dependencies into the build environment (or use --no-build-isolation and pip install them first).","Read the captured traceback to find the precise ImportError and fix the named missing module."],"exampleFix":"# before\nbuild-backend = \"flit_core.buildapi\"\nrequires = [\"wheel\"]\n\n# after\nbuild-backend = \"flit_core.buildapi\"\nrequires = [\"flit_core>=3.2\", \"wheel\"]","handlingStrategy":"try-catch","validationCode":"import importlib.util\nmod = build_backend.split(':')[0]\nif importlib.util.find_spec(mod) is None:\n    raise RuntimeError(f'backend module {mod} is not installed')","typeGuard":null,"tryCatchPattern":"try:\n    backend = _build_backend()\nexcept BackendUnavailable as e:\n    log.error('cannot import backend %s: %s', e.message, e.traceback)\n    raise","preventionTips":["Pin and install the backend in build-system.requires.","Smoke-test importlib.import_module of the backend before invoking hooks."],"tags":["pyproject-hooks","pep517","build","import","dependencies"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}