{"record":{"id":"8d2fbdb865b9e364","repo":"pypa/pip","slug":"pip-can-t-proceed-with-requirements-self-due-t","errorCode":null,"errorMessage":"pip can't proceed with requirements '{self}' due to a pre-existing build directory ({self.source_dir}). This is likely due to a previous installation that failed . pip is being responsible and not assuming it can delete this. Please delete it and try again.","messagePattern":"pip can't proceed with requirements '(.+?)' due to a pre-existing build directory \\((.+?)\\)\\. This is likely due to a previous installation that failed \\. pip is being responsible and not assuming it can delete this\\. Please delete it and try again\\.","errorType":"exception","errorClass":"PreviousBuildDirError","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/req/req_install.py","lineNumber":628,"sourceCode":"                parent_dir,\n                autodelete=autodelete,\n                parallel_builds=parallel_builds,\n            )\n\n    def needs_unpacked_archive(self, archive_source: Path) -> None:\n        assert self._archive_source is None\n        self._archive_source = archive_source\n\n    def ensure_pristine_source_checkout(self) -> None:\n        \"\"\"Ensure the source directory has not yet been built in.\"\"\"\n        assert self.source_dir is not None\n        if self._archive_source is not None:\n            unpack_file(str(self._archive_source), self.source_dir)\n        elif is_installable_dir(self.source_dir):\n            # If a checkout exists, it's unwise to keep going.\n            # version inconsistencies are logged later, but do not fail\n            # the installation.\n            raise PreviousBuildDirError(\n                f\"pip can't proceed with requirements '{self}' due to a \"\n                f\"pre-existing build directory ({self.source_dir}). This is likely \"\n                \"due to a previous installation that failed . pip is \"\n                \"being responsible and not assuming it can delete this. \"\n                \"Please delete it and try again.\"\n            )\n\n    # For editable installations\n    def update_editable(self) -> None:\n        if not self.link:\n            logger.debug(\n                \"Cannot update repository at %s; repository location is unknown\",\n                self.source_dir,\n            )\n            return\n        assert self.editable\n        assert self.source_dir\n        if self.link.scheme == \"file\":","sourceCodeStart":610,"sourceCodeEnd":646,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_internal/req/req_install.py#L610-L646","documentation":"ensure_pristine_source_checkout (req_install.py:628) raises PreviousBuildDirError when the source_dir already looks installable (is_installable_dir returns true), indicating a prior build left artifacts behind. pip refuses to assume it can safely delete the directory, to avoid version inconsistencies from a half-built tree.","triggerScenarios":"A previous pip build/install failed or was interrupted, leaving build artifacts in the unpacked source dir; re-running pip install against the same checkout triggers the check.","commonSituations":"Interrupted builds in CI with cached source dirs; local dev re-running pip after a failed -e install; stale build/ directories.","solutions":["Delete the stale build/source directory named in the error and retry.","Run pip cache purge and pip install --no-cache-dir to avoid reusing tainted dirs.","Use a fresh checkout or build in a clean tmp directory."],"exampleFix":"# before (stale dir blocks install)\npip install ./pkg\n# after\nrm -rf ./pkg/build ./build && pip install ./pkg","handlingStrategy":"validation","validationCode":"# Before installing from a path, ensure no stale build dir exists.\nimport pathlib, shutil\nsrc = pathlib.Path(\"./pkg\")\nfor stale in [src / \"build\", pathlib.Path(\"build\"), src / \"pip-build\"]:\n    if stale.is_dir():\n        shutil.rmtree(stale)\n        print(f\"Removed stale build dir: {stale}\")","typeGuard":null,"tryCatchPattern":"# When scripting pip, detect PreviousBuildDirError and clean up once.\nimport subprocess, re, sys\nproc = subprocess.run([sys.executable, \"-m\", \"pip\", \"install\", \"./pkg\"], capture_output=True, text=True)\nif \"pre-existing build directory\" in proc.stderr:\n    m = re.search(r\"\\((.*?)\\)\", proc.stderr)\n    if m:\n        import shutil; shutil.rmtree(m.group(1), ignore_errors=True)\n    # retry once\n    subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", \"./pkg\"])","preventionTips":["Clean build/ directories after failed installs.","Use pip install --no-cache-dir in CI to avoid tainted build dirs.","Build in ephemeral tmp dirs to guarantee a pristine state."],"tags":["build-dir","stale-state","cleanup"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}