{"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/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_internal/req/req_install.py#L610-L646","documentation":"PreviousBuildDirError raised by InstallRequirement.ensure_pristine_source_checkout when the source_dir for a requirement already contains an installable checkout/build artefact. After a failed previous install, pip refuses to silently delete the leftover build directory (it might contain user changes or partial state) and asks the user to remove it explicitly. This avoids corrupting a half-built tree.","triggerScenarios":"A prior 'pip install' of the same sdist/VCS requirement failed midway, leaving <build_dir>/<req>/ populated and installable; rerunning pip hits ensure_pristine_source_checkout and is_installable_dir returns True. Common with retries after a build error or an interrupted Ctrl-C.","commonSituations":"Ctrl-C during 'pip install'; backend crashed during build; switching branches in a '-e git+...' editable that left a stale checkout; CI retry on the same workspace without cleanup; pip's --no-clean keeping the build dir.","solutions":["Delete the offending directory named in the message (self.source_dir): 'rm -rf <path>'.","Clear pip's global build/cache dirs: 'pip cache purge' and remove /tmp pip-* build dirs.","If it is a VCS editable, also remove the project's .egg-info and any src/*.egg-link.","Re-run pip; if it fails again, capture the underlying build error before retrying."],"exampleFix":"# before\npip install ./myproject   # failed once, retry fails with PreviousBuildDirError\n\n# after\nrm -rf /tmp/pip-*/myproject   # or the path from the message\npip install ./myproject","handlingStrategy":"validation","validationCode":"import os, glob\ndef stale_build_dirs() -> list[str]:\n    return [d for d in glob.glob('/tmp/pip-*') + glob.glob(os.path.expanduser('~/.cache/pip')) if os.path.isdir(d)]","typeGuard":"def has_stale_build_dir(source_dir: str) -> bool:\n    import os\n    from pip._internal.utils.misc import is_installable_dir\n    return os.path.isdir(source_dir) and is_installable_dir(source_dir)","tryCatchPattern":"from pip._internal.exceptions import PreviousBuildDirError\ntry:\n    run_pip(['install', './myproject'])\nexcept PreviousBuildDirError as e:\n    print(f'removing stale build dir: {e}'); import shutil, re; shutil.rmtree(re.search(r'\\(([^)]+)\\)', str(e)).group(1), ignore_errors=True)","preventionTips":["Clean build dirs between failed retries: 'pip cache purge'.","In CI start from a fresh workspace or rm -rf build/ before pip.","Investigate the original build failure before deleting state."],"tags":["build-dir","install","cleanup","state"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}