python encoding_error ai_generated true

UnicodeDecodeError:'utf-8' 编解码器无法解码位置 0 中的字节 0xff:无效的起始字节

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

ID: python/pip-freeze-unicode-decode-error

其他格式: JSON · Markdown 中文 · English
80%修复率
82%置信度
0证据数
2024-08-04首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

某个 site-packages 下的 .dist-info METADATA 或 RECORD 文件以非 UTF-8 编码写入(Windows 上常为 cp1252 或 UTF-16),而 pip freeze/install 尝试以 UTF-8 读取。

English

A site-packages .dist-info METADATA or RECORD file was written with a non-UTF-8 encoding (often cp1252 or UTF-16 on Windows), and pip freeze/install tries to read it as UTF-8.

generic

解决方案

  1. 90% 成功率
    python - <<'PY'
    import pathlib, sys
    for p in pathlib.Path(sys.prefix).rglob('METADATA'):
        try:
            p.read_text(encoding='utf-8')
        except UnicodeDecodeError:
            print('BAD:', p)
    PY
    # then uninstall the owning package
    python -m pip uninstall -y badpkg
  2. 85% 成功率
    python -m pip install --force-reinstall --no-cache-dir badpkg

无效尝试

常见但无效的做法:

  1. 85% 失败

    Affects stdout/stderr only, not the file-reading path that raises the error.

  2. 90% 失败

    The corrupt file belongs to another package's metadata, not pip itself.