pypa/pip · error · CommandError

Target path exists but is not a directory, will not…

Error message

Target path exists but is not a directory, will not continue.

What it means

Raised by `pip install --target <path>` when the path exists but is a regular file (not a directory). InstallCommand.run at install.py:416 checks `os.path.exists(target_dir) and not os.path.isdir(target_dir)`; a file at that location makes installation impossible and is rejected before any work begins.

Solutions

  1. Choose a target path that is either a directory or does not exist: `--target ./libs`.
  2. If a file is wrongly there, remove or rename it first: `rm <file>` then retry.
  3. Verify with `ls -ld <path>` that the target is a directory before running.

Example fix

// before
pip install --target libs.zip numpy   # libs.zip is a file
// after
rm libs.zip
pip install --target ./libs numpy
Defensive patterns

Strategy: validation

Validate before calling

# Verify --target is absent or a directory before pip install
import os

def target_dir_ok(path):
    if path is None:
        return True
    if os.path.exists(path) and not os.path.isdir(path):
        raise ValueError("--target %s exists and is not a directory" % path)
    return True

Prevention

When it happens

Trigger: Passing --target a path that already exists as a file (e.g. `--target requirements.txt` or a path where a file was accidentally created).

Common situations: Reusing a filename as a target dir, a typo pointing --target at an existing file, or a prior misdirected command created a file where a directory was expected.

Related errors


AI-assisted analysis of pypa/pip@f399c37189 (2026-08-08). Data as JSON: /api/errors/9fb4d734bd5c1b8b. Report an issue: GitHub.

Appendix: source

Thrown at src/pip/_internal/commands/install.py:422

            options.use_user_site,
            prefix_path=options.prefix_path,
            target_dir=options.target_dir,
            root_path=options.root_path,
            isolated_mode=options.isolated_mode,
        )

        target_temp_dir: TempDirectory | None = None
        target_temp_dir_path: str | None = None
        if options.target_dir:
            options.ignore_installed = True
            options.target_dir = os.path.abspath(options.target_dir)
            if (
                # fmt: off
                os.path.exists(options.target_dir) and
                not os.path.isdir(options.target_dir)
                # fmt: on
            ):
                raise CommandError(
                    "Target path exists but is not a directory, will not continue."
                )

            # Create a target directory for using with the target option
            target_temp_dir = TempDirectory(kind="target")
            target_temp_dir_path = target_temp_dir.path
            self.enter_context(target_temp_dir)

        session = self.get_default_session(options)

        target_python = make_target_python(options)
        finder = self._build_package_finder(
            options=options,
            session=session,
            target_python=target_python,
            ignore_requires_python=options.ignore_requires_python,
        )
        build_tracker = self.enter_context(get_build_tracker())

View on GitHub (pinned to f399c37189)