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

- **ID:** `python/pip-freeze-unicode-decode-error`
- **领域:** python
- **类别:** encoding_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

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
   ```

## 无效尝试

- **** — Affects stdout/stderr only, not the file-reading path that raises the error. (85% 失败率)
- **** — The corrupt file belongs to another package's metadata, not pip itself. (90% 失败率)
