{"record":{"id":"b315c2007164666f","repo":"pypa/pip","slug":"invalid-pylock-file-pylock-path-or-url-r-exc","errorCode":null,"errorMessage":"Invalid pylock file {pylock_path_or_url!r}: {exc}","messagePattern":"Invalid pylock file (.+?): (.+?)","errorType":"exception","errorClass":"InstallationError","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/utils/pylock.py","lineNumber":288,"sourceCode":") -> Iterator[\n    tuple[\n        Package,\n        PackageVcs | PackageDirectory | PackageArchive | PackageWheel | PackageSdist,\n    ]\n]:\n    try:\n        pylock_content = _get_pylock_path_or_url_content(pylock_path_or_url, session)\n    except DiagnosticPipError:\n        raise\n    except Exception as exc:\n        raise InstallationError(\n            f\"Error reading pylock file {pylock_path_or_url!r}: {exc}\"\n        ) from exc\n\n    try:\n        lock = Pylock.from_dict(tomllib.loads(pylock_content))\n    except Exception as exc:\n        raise InstallationError(\n            f\"Invalid pylock file {pylock_path_or_url!r}: {exc}\"\n        ) from exc\n\n    try:\n        # TODO: for completeness, pylock.select should support preferring sdist\n        # over wheels to support --no-binary\n        yield from lock.select()\n    except Exception as exc:\n        raise InstallationError(\n            f\"Cannot select requirements from pylock file {pylock_path_or_url!r}: {exc}\"\n        ) from exc\n","sourceCodeStart":270,"sourceCodeEnd":300,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_internal/utils/pylock.py#L270-L300","documentation":"Raised as InstallationError by select_from_pylock_path_or_url (pylock.py:288) when the pylock file content was read successfully but fails to parse or validate against the pylock schema. The code at line 286 calls tomllib.loads to parse TOML and Pylock.from_dict to validate; any exception from either (malformed TOML, missing required fields, wrong types) is caught at line 287 and re-raised with context. The original exception is chained.","triggerScenarios":"Loading a pylock.toml that has TOML syntax errors (unquoted keys, bad escaping), is not a valid pylock file (missing required top-level fields like lock_version, packages), or has package entries that fail Pylock.from_dict schema validation. The except at line 287 catches it.","commonSituations":"A hand-edited lock file with TOML syntax errors. A lock file generated by an incompatible or outdated tool. A partially-written or truncated lock file. Schema version mismatch (lock_version not supported).","solutions":["Validate the TOML syntax independently: `python -c \"import tomllib; tomllib.load(open('pylock.toml','rb'))\"`.","Check the error message (from the chained exception) for the specific field or syntax problem and fix it.","Regenerate the lock file with a compatible tool (`pip lock` or pip-tools with pylock support).","Ensure the lock_version in the file matches what your pip version supports (currently 1.0)."],"exampleFix":"// before (invalid TOML)\nlock-version = 1.0\n[[packages\nname = \"pkg\"\n\n// after\nlock-version = \"1.0\"\n[[packages]]\nname = \"pkg\"","handlingStrategy":"validation","validationCode":"import tomllib\nfrom pathlib import Path\n\ndef validate_pylock_toml(path: str) -> list[str]:\n    \"\"\"Pre-validate a pylock.toml's TOML syntax; return list of errors.\"\"\"\n    errors = []\n    try:\n        data = tomllib.loads(Path(path).read_text(encoding='utf-8'))\n    except tomllib.TOMLDecodeError as e:\n        errors.append(f'TOML syntax error: {e}')\n        return errors\n    for key in ('lock-version', 'packages'):\n        if key not in data:\n            errors.append(f'Missing required field: {key}')\n    return errors","typeGuard":"def is_valid_pylock_toml(path: str) -> bool:\n    \"\"\"True if the file parses as valid TOML with required pylock keys.\"\"\"\n    try:\n        data = tomllib.loads(Path(path).read_text(encoding='utf-8'))\n        return 'lock-version' in data and 'packages' in data\n    except Exception:\n        return False","tryCatchPattern":"from pip._internal.exceptions import InstallationError\n\ntry:\n    # pip install -r pylock.toml\n    pass\nexcept InstallationError as e:\n    if 'Invalid pylock file' in str(e):\n        # parse error: validate TOML independently and fix schema\n        pass","preventionTips":["Validate TOML syntax with a standalone parser before installing from lock files.","Use a schema validator for pylock files in your CI pipeline.","Regenerate lock files with supported tools rather than hand-editing them."],"tags":["pylock","toml","schema-validation","parse-error","validation"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}