pypa/pip · error · InvalidInstalledPackage

invalid-installed-package

invalid-installed-package

Error message

Cannot process installed package {dist} in {installed_location!r} because it has an invalid {invalid_type}:
{invalid_exc.args[0]}

What it means

In Factory._make_requirement_from_install_req (factory.py:293-299), when checking whether the installed version satisfies the specifier, specifier.contains() can raise InvalidVersion if the installed dist's version string isn't PEP 440 compliant. pip wraps this as InvalidInstalledPackage (code invalid-installed-package, invalid_type='version'). Pip 24.1+ enforces strict PEP 440 parsing.

Source

Thrown at src/pip/_internal/resolution/resolvelib/factory.py:299

        def _get_installed_candidate() -> Candidate | None:
            """Get the candidate for the currently-installed version."""
            # If --force-reinstall is set, we want the version from the index
            # instead, so we "pretend" there is nothing installed.
            if self._force_reinstall:
                return None
            try:
                installed_dist = self._installed_dists[name]
            except KeyError:
                return None

            try:
                # Don't use the installed distribution if its version
                # does not fit the current dependency graph.
                if not specifier.contains(installed_dist.version, prereleases=True):
                    return None
            except InvalidVersion as e:
                raise InvalidInstalledPackage(dist=installed_dist, invalid_exc=e)

            candidate = self._make_candidate_from_dist(
                dist=installed_dist,
                extras=extras,
                template=template,
            )
            # The candidate is a known incompatibility. Don't use it.
            if id(candidate) in incompatible_ids:
                return None
            return candidate

        def iter_index_candidate_infos() -> Iterator[IndexCandidateInfo]:
            result = self._finder.find_best_candidate(
                project_name=name,
                specifier=specifier,
                hashes=hashes,
            )
            icans = result.applicable_candidates

View on GitHub (pinned to f399c37189)

Solutions

  1. Uninstall the package with the malformed version.
  2. Reinstall a version of the package that uses a PEP 440 compliant version string.
  3. Temporarily pin pip below 24.1 while migrating away from the bad package.

Example fix

# before
pip install dep   # fails: installed badpkg version '1.0x' is invalid
# after
pip uninstall badpkg && pip install badpkg==1.0 && pip install dep
Defensive patterns

Strategy: validation

Validate before calling

# Scan installed packages for non-PEP 440 versions before resolving.
import importlib.metadata as md
from packaging.version import InvalidVersion, parse
for dist in md.distributions():
    try:
        parse(dist.version)
    except InvalidVersion:
        print(f"INVALID version {dist.version!r} for {dist.metadata['Name']}; uninstall before proceeding")

Prevention

When it happens

Trigger: An installed package has a version string that doesn't conform to PEP 440 (e.g. '1.0-dev-snapshot'); the resolvelib factory tries to compare it against a specifier and InvalidVersion is raised.

Common situations: Packages with legacy/custom version schemes installed by older pip; upgrading pip to 24.1+ surfaces previously-tolerated bad versions.

Related errors


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