{"record":{"id":"ed04b9353f04c36b","repo":"pypa/pip","slug":"bad-metadata-in-dist-invalid-metadata-entry-ve","errorCode":null,"errorMessage":"Bad metadata in {dist} (invalid metadata entry `version`)","messagePattern":"Bad metadata in (.+?) \\(invalid metadata entry `version`\\)","errorType":"exception","errorClass":"BadMetadata","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/metadata/importlib/_dists.py","lineNumber":176,"sourceCode":"    def installed_location(self) -> str | None:\n        if self._installed_location is None:\n            return None\n        return normalize_path(str(self._installed_location))\n\n    @property\n    def canonical_name(self) -> NormalizedName:\n        return get_dist_canonical_name(self._dist)\n\n    @property\n    def version(self) -> Version:\n        try:\n            version = (\n                parse_name_and_version_from_info_directory(self._dist)[1]\n                or self._dist.version\n            )\n            return parse_version(version)\n        except TypeError:\n            raise BadMetadata(self._dist, reason=\"invalid metadata entry `version`\")\n\n    @property\n    def raw_version(self) -> str:\n        return self.metadata[\"Version\"]\n\n    def is_file(self, path: InfoPath) -> bool:\n        return self._dist.read_text(str(path)) is not None\n\n    def iter_distutils_script_names(self) -> Iterator[str]:\n        # A distutils installation is always \"flat\" (not in e.g. egg form), so\n        # if this distribution's info location is NOT a pathlib.Path (but e.g.\n        # zipfile.Path), it can never contain any distutils scripts.\n        if not isinstance(self._info_location, pathlib.Path):\n            return\n        for child in self._info_location.joinpath(\"scripts\").iterdir():\n            yield child.name\n\n    def read_text(self, path: InfoPath) -> str:","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_internal/metadata/importlib/_dists.py#L158-L194","documentation":"Raised as BadMetadata(dist, reason=\"invalid metadata entry `version`\") from the Distribution.version property in _dists.py:168-176. After trying to parse the version from the info directory name and falling back to dist.version, pip calls parse_version(version); if that raises TypeError (version is None/non-string), it is wrapped as BadMetadata because a valid PEP 440 version string is mandatory.","triggerScenarios":"Accessing the .version property of an importlib-backed Distribution whose info dir has no version AND whose dist.version is None or not a string, so parse_version raises TypeError.","commonSituations":"A distribution whose METADATA lacks a 'Version' field; a malformed .dist-info directory name without a version segment; metadata damaged by a faulty installer.","solutions":["Reinstall the package from a good source to restore a valid 'Version' in METADATA.","Repair or delete the corrupt .dist-info directory under site-packages.","When building, ensure the backend writes a valid PEP 440 version string."],"exampleFix":"// before: METADATA missing Version\nMetadata-Version: 2.1\nName: mypkg\n// after\nMetadata-Version: 2.1\nName: mypkg\nVersion: 1.0.0","handlingStrategy":"try-catch","validationCode":"// Before reading a dist's version, sanity-check the Version field:\nfor d in importlib.metadata.distributions():\n    v = d.metadata.get('Version')\n    if not v:\n        print('dist missing Version:', d)","typeGuard":null,"tryCatchPattern":"from pip._internal.metadata.importlib._compat import BadMetadata\ntry:\n    ver = dist.version\nexcept BadMetadata as e:\n    if 'version' in e.reason:\n        # reinstall or skip the broken dist\n        ...","preventionTips":["Always emit a valid PEP 440 'Version' in METADATA.","Audit installed dists for missing version fields.","Reinstall packages pip reports as having bad version metadata."],"tags":["metadata","version","bad-metadata","importlib"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}