# UnicodeDecodeError: 'charmap' 编解码器无法解码位置 1234 处的字节 0x9a：字符映射到 <undefined>

- **ID:** `python/pip-requirements-file-encoding-error`
- **领域:** python
- **类别:** encoding_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

requirements.txt 文件包含非 UTF-8 编码的字符（例如 Windows-1252），pip 尝试使用系统默认编码解码该文件。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |

## 解决方案

1. **Re-save requirements.txt with UTF-8 encoding using a proper editor** (95% 成功率)
   ```
   Open file in VS Code or Sublime, click 'Save with Encoding' and choose UTF-8, then re-run `pip install -r requirements.txt`
   ```
2. **Force pip to use UTF-8 encoding by setting PYTHONUTF8 environment variable** (85% 成功率)
   ```
   On Windows: `set PYTHONUTF8=1` then `pip install -r requirements.txt`. On Linux/macOS: `PYTHONUTF8=1 pip install -r requirements.txt`
   ```

## 无效尝试

- **Manually editing the file in Notepad and saving as ANSI** — Notepad's 'ANSI' encoding is often Windows-1252, which may still contain problematic bytes for other locales. (70% 失败率)
- **Using `pip install -r requirements.txt --no-cache`** — The --no-cache flag does not affect file decoding; the error occurs during file reading, not caching. (90% 失败率)
