pypa/pip · error · InvalidSdistFilename

Invalid sdist filename

Error message

Invalid sdist filename: {filename!r}

What it means

Raised by parse_sdist_filename() when the stem (after removing the extension) contains no dash at all (utils.py:353-355). The parser uses rpartition('-') to split name from version; if no dash exists, sep is empty, meaning there is no name-version boundary. The exception class is InvalidSdistFilename.

Solutions

  1. Ensure the filename contains a dash between the project name and the version.
  2. Rename the file to follow the {name}-{version}.tar.gz convention.
  3. Verify the source of the filename is an actual PyPI-style sdist.

Example fix

// before
parse_sdist_filename('foo10.tar.gz')
// after
parse_sdist_filename('foo-1.0.tar.gz')
Defensive patterns

Strategy: validation

Validate before calling

def safe_parse_sdist(filename: str):
    stem = filename[:-7] if filename.endswith(".tar.gz") else filename[:-4]
    if "-" not in stem:
        raise ValueError(f"Sdist filename has no name-version dash: {filename!r}")
    return parse_sdist_filename(filename)

Type guard

def has_sdist_dash(filename: str) -> bool:
    if not isinstance(filename, str):
        return False
    if filename.endswith(".tar.gz"):
        stem = filename[:-7]
    elif filename.endswith(".zip"):
        stem = filename[:-4]
    else:
        return False
    return "-" in stem

Try / catch

from packaging.utils import InvalidSdistFilename

try:
    name, ver = parse_sdist_filename(filename)
except InvalidSdistFilename as e:
    if "Invalid sdist filename:" in str(e) and "empty" not in str(e):
        logger.error("No name-version separator in: %s", filename)
    raise

Prevention

When it happens

Trigger: A filename with no dash separating the project name from the version, e.g. parse_sdist_filename('foo10.tar.gz') where the stem 'foo10' has no dash.

Common situations: A filename where the name and version were concatenated without a dash, or a stray file that happens to have a .tar.gz extension but is not a valid sdist.

Related errors


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

Appendix: source

Thrown at src/pip/_vendor/packaging/utils.py:355

       Raises :class:`InvalidSdistFilename` on an empty project name.

    .. _Source distribution format: https://packaging.python.org/specifications/source-distribution-format/#source-distribution-file-name
    """
    if filename.endswith(".tar.gz"):
        file_stem = filename[: -len(".tar.gz")]
    elif filename.endswith(".zip"):
        file_stem = filename[: -len(".zip")]
    else:
        raise InvalidSdistFilename(
            f"Invalid sdist filename (extension must be '.tar.gz' or '.zip'):"
            f" {filename!r}"
        )

    # We are requiring a PEP 440 version, which cannot contain dashes,
    # so we split on the last dash.
    name_part, sep, version_part = file_stem.rpartition("-")
    if not sep:
        raise InvalidSdistFilename(f"Invalid sdist filename: {filename!r}")
    if not name_part:
        raise InvalidSdistFilename(
            f"Invalid sdist filename (empty project name): {filename!r}"
        )

    name = canonicalize_name(name_part)

    try:
        version = Version(version_part)
    except InvalidVersion as e:
        raise InvalidSdistFilename(
            f"Invalid sdist filename (invalid version): {filename!r}"
        ) from e

    return (name, version)

View on GitHub (pinned to f399c37189)