{"id":"9fb4d734bd5c1b8b","repo":"pypa/pip","slug":"target-path-exists-but-is-not-a-directory-will-no","errorCode":null,"errorMessage":"Target path exists but is not a directory, will not continue.","messagePattern":"Target path exists but is not a directory, will not continue\\.","errorType":"exception","errorClass":"CommandError","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/commands/install.py","lineNumber":422,"sourceCode":"            options.use_user_site,\n            prefix_path=options.prefix_path,\n            target_dir=options.target_dir,\n            root_path=options.root_path,\n            isolated_mode=options.isolated_mode,\n        )\n\n        target_temp_dir: TempDirectory | None = None\n        target_temp_dir_path: str | None = None\n        if options.target_dir:\n            options.ignore_installed = True\n            options.target_dir = os.path.abspath(options.target_dir)\n            if (\n                # fmt: off\n                os.path.exists(options.target_dir) and\n                not os.path.isdir(options.target_dir)\n                # fmt: on\n            ):\n                raise CommandError(\n                    \"Target path exists but is not a directory, will not continue.\"\n                )\n\n            # Create a target directory for using with the target option\n            target_temp_dir = TempDirectory(kind=\"target\")\n            target_temp_dir_path = target_temp_dir.path\n            self.enter_context(target_temp_dir)\n\n        session = self.get_default_session(options)\n\n        target_python = make_target_python(options)\n        finder = self._build_package_finder(\n            options=options,\n            session=session,\n            target_python=target_python,\n            ignore_requires_python=options.ignore_requires_python,\n        )\n        build_tracker = self.enter_context(get_build_tracker())","sourceCodeStart":404,"sourceCodeEnd":440,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_internal/commands/install.py#L404-L440","documentation":"Raised in InstallCommand.run() at install.py:416-424 when the path passed to --target exists on the filesystem but is not a directory (it is a regular file, a symlink to a file, a device node, etc.). pip needs a real directory to move installed package files into, so it refuses to overwrite a non-directory path.","triggerScenarios":"Running 'pip install --target <path>' where <path> resolves via os.path.exists() but fails os.path.isdir(). Commonly <path> is an existing file, a broken or file-pointing symlink, or a named pipe.","commonSituations":"Typing a filename instead of a directory name for --target; a previous build created a file at that path; using --target ./requirements.txt by confusing it with -r; leftover symlinks from a broken setup.","solutions":["Remove or rename the existing non-directory path, then re-run.","Choose a different --target path that is a directory or does not yet exist (pip will create it).","If it is a stale symlink, delete it with 'rm <path>' first.","Verify the path with 'ls -la <path>' to confirm its type before re-running."],"exampleFix":"# before (./build is a regular file)\npip install --target ./build requests\n# after\nrm ./build && pip install --target ./build requests","handlingStrategy":"validation","validationCode":"import os, sys\n\ntarget = None\nfor i, a in enumerate(sys.argv):\n    if a in ('-t','--target') and i+1 < len(sys.argv):\n        target = sys.argv[i+1]\n    elif a.startswith('--target='):\n        target = a.split('=',1)[1]\nif target and os.path.exists(target) and not os.path.isdir(target):\n    print(f'ERROR: --target {target} is not a directory', file=sys.stderr)\n    sys.exit(2)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify the --target path is a directory or does not exist before running pip install.","Use os.path.isdir (not just os.path.exists) in pre-flight checks.","Choose target paths that do not collide with existing files."],"tags":["pip","install","target","filesystem","path"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}