使用python3 build进行Sublime Text 2编码错误

时间:2013-03-01 20:13:06

标签: python character-encoding sublimetext2

从Sublime Text 2运行我的python3脚本时,会发生以下错误:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

此外,从终端运行相同的脚本时,问题不会出现。

Sublime的构建系统设置如下:

{
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "utf8",
"path": "/Library/Frameworks/Python.framework/Versions/3.3/bin/"
}

提前致谢!

2 个答案:

答案 0 :(得分:28)

经过一番调查和研究,我发现了问题所在:

子进程中缺少LANG env变量,由Sublime Text 2运行。我通过在构建设置中添加LANG变量来修复它,如下所示:

{
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"LANG": "en_US.UTF-8"}
}

答案 1 :(得分:1)

Martin的解决方案非常棒!在Windows / Python3.5环境中,而不是" LANG"设置以下内容:

"env": {"PYTHONIOENCODING": "utf8"}