如题:
环境:x86 Windows 10 32位,sublime 4152 Python 3.8.7rc1 ;
x86 Windows 11 64位,sublime 4152 Python 3.11.4。
默认安装python和sublime后,运行以下代码:
import os, sys
print('你好')
print(sys.stdout.encoding)
os.system('你好')
结果:
你好
utf-8
'���' �����ڲ����ⲿ���Ҳ���ǿ����еij���
���������ļ���
[Finished in 137ms]
使用WinRAR打开安装路径下的文件:C:\Program Files\Sublime Text\Packages\Python.sublime-package,查看其中的Python.sublime-build文件:
{
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
"windows": {
"cmd": ["py", "-u", "$file"],
},
"variants":
[
{
"name": "Syntax Check",
"cmd": ["python3", "-m", "py_compile", "$file"],
"windows": {
"cmd": ["py", "-m", "py_compile", "$file"],
}
}
]
}
修改为如下:
{
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
// "env": {"PYTHONIOENCODING": "utf-8"}, //源代码
// "env": {"PYTHONIOENCODING": "utf8"}, //网文提供的解决方案 A,问题依旧
// "env": {"LANG": "en_US.utf-8"}, //网文提供的解决方案 B,问题依旧
"encoding": "gbk", //问题解决
"windows": {
"cmd": ["py", "-u", "$file"],
},
"variants":
[
{
"name": "Syntax Check",
"cmd": ["python3", "-m", "py_compile", "$file"],
"windows": {
"cmd": ["py", "-m", "py_compile", "$file"],
}
}
]
}
关闭并重新运行sublime,结果:
你好
gbk
'你好' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
[Finished in 136ms]
问题解决。