Skip to content

Windows 中文乱码解决方案

本文整理自 GitHub openai/codex 社区 Issue(#4498、#4574、#7290、#9580、#14311 等),汇总了乱码问题的根因分析与目前可用的修复方案。

🔍 为什么会出现乱码?

Codex 在 Windows 上启动 PowerShell 子进程时,硬编码了 -NoProfile 参数,导致你在 PowerShell Profile 里配置的 UTF-8 编码设置被完全跳过

子进程回退到系统默认代码页(中文 Windows 通常是 CP936 / GB2312),而 Codex 写文件用的是 UTF-8。UTF-8 文本经过 GB2312 解码,就变成了乱码(mojibake)。

简单说:系统编码 ≠ Codex 期望的 UTF-8,两者不一致就出现乱码。

这个问题从 2025 年 9 月起陆续在社区报告,截至目前(2026-03)官方尚未彻底修复。


✅ 推荐解决方案(按效果排序)

方案一:开启 Windows 全局 UTF-8(最推荐)

这是最彻底的方案,直接把系统默认代码页改为 UTF-8,从根源解决问题。

操作步骤:

  1. 打开「控制面板」→「区域」
  2. 切换到「管理」选项卡
  3. 点击「更改系统区域设置」
  4. 勾选 「Beta 版: 使用 Unicode UTF-8 提供全球语言支持」
  5. 重启电脑

⚠️ 副作用:极少数旧软件(尤其是 10 年以上的老程序)可能出现界面乱码,现代软件基本不受影响。


方案二:安装 PowerShell 7(推荐搭配方案一)

Windows 自带的 PowerShell 5.1 默认编码是 ANSI,而 PowerShell 7(pwsh)默认就是 UTF-8

安装 PowerShell 7:

powershell
winget install Microsoft.PowerShell

安装后,在 VS Code / 终端设置里把默认 Shell 改为 pwsh.exe


方案三:在 Codex 指令中加编码安全提示(无需改系统)

如果你不想修改系统设置,可以通过「告诉 Codex 避开会出现乱码的命令」来绕过问题。

在 Codex 的 Agent Instructions(系统提示)里加入以下内容:

Encoding Safety:
- Do NOT use PowerShell Get-Content / Set-Content / -replace to edit project files.
  These commands default to CP1252 and will corrupt UTF-8 files.
- For all file read/write operations, use `apply_patch` or Python with explicit UTF-8:
  python - <<'PY'
  with open("file.txt", "r", encoding="utf-8") as f:
      content = f.read()
  # modify content ...
  with open("file.txt", "w", encoding="utf-8") as f:
      f.write(content)
  PY

这个方法引导 LLM 绕开 PowerShell 文件读写,转用 apply_patch 或 Python 显式指定 encoding="utf-8" 来操作文件。社区反馈效果不错,是不想改系统时的最佳折中方案。


方案四:设置环境变量(Python SDK 用户)

如果你通过 Python SDK 使用 Codex,可以设置以下环境变量强制 Python 使用 UTF-8:

powershell
# 临时生效(当前会话)
$env:PYTHONUTF8 = "1"
$env:PYTHONIOENCODING = "utf-8"

# 永久生效(写入系统环境变量)
[System.Environment]::SetEnvironmentVariable("PYTHONUTF8", "1", "User")
[System.Environment]::SetEnvironmentVariable("PYTHONIOENCODING", "utf-8", "User")

方案五:PowerShell Profile 配置 UTF-8

在 PowerShell Profile 中强制设置 UTF-8 编码:

powershell
# 编辑 Profile 文件
notepad $PROFILE

加入以下内容:

powershell
# 强制 UTF-8 无 BOM
chcp 65001 | Out-Null
[Console]::InputEncoding  = [System.Text.UTF8Encoding]::new($false)
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)
$OutputEncoding = [System.Text.UTF8Encoding]::new($false)

⚠️ 局限性:因为 Codex 用 -NoProfile 启动 PowerShell,这个配置对 Codex 直接调用的场景无效。但如果你在 VS Code 内置终端里手动操作,这个配置还是有用的。


方案六:VS Code 设置(辅助)

在 VS Code 的 settings.json 中加入:

json
{
  "files.encoding": "utf8",
  "files.autoGuessEncoding": true,
  "terminal.integrated.defaultProfile.windows": "PowerShell"
}

这能保证编辑器侧读写文件都用 UTF-8,但不能解决 Codex 通过终端写文件时的编码问题。属于必要但不充分的配置,和其他方案搭配使用。


🎯 推荐组合

场景推荐方案
想一劳永逸方案一(全局 UTF-8)+ 方案二(PowerShell 7)
不想改系统方案三(Agent 指令)+ 方案六(VS Code 配置)
Python SDK 用户方案四(环境变量)+ 方案三

🐛 apply_patch 崩溃问题

如果你遇到 Codex 在处理含中文的文件时直接 panic 崩溃(而不是乱码),这是另一个 bug(Issue #9580):apply_patch 工具在处理多字节字符时会触发 byte boundary panic。

临时解决方法:在 system prompt 中指示 Codex 不要使用 apply_patch,改用 Python 脚本显式写文件(参考方案三的示例代码)。


📌 总结

这个问题的根本原因需要 OpenAI 在 Codex 源码中显式强制 UTF-8 编码,而非依赖系统代码页,目前官方修复还未完全落地。

在等待官方修复的同时,开启 Windows 全局 UTF-8(方案一) 是最彻底的用户侧解决方案;如果不想改系统,在 Agent Instructions 中加入编码安全提示(方案三) 是最实用的折中方案。