python encoding_error ai_generated true

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

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9a in position 1234: character maps to <undefined>

ID: python/pip-requirements-file-encoding-error

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

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active
3.10 active
3.11 active

根因分析

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

English

A requirements.txt file contains non-UTF-8 encoded characters (e.g., Windows-1252), and pip attempts to decode it using the system's default encoding.

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. Manually editing the file in Notepad and saving as ANSI 70% 失败

    Notepad's 'ANSI' encoding is often Windows-1252, which may still contain problematic bytes for other locales.

  2. Using `pip install -r requirements.txt --no-cache` 90% 失败

    The --no-cache flag does not affect file decoding; the error occurs during file reading, not caching.