解决WIN7下Sublime运行python代码出现的UnicodeEncodeError错误

  • by

在win7下安装sublime后,编写python脚本并执行,没有任何反应,ctrl+`打开控制台之后才发现执行程序时出现了错误:

Running python.exe -u E:\文档\test.py
 Traceback (most recent call last):
 File ".\sublime_plugin.py", line 337, in run_
 return self.run(**args)
 File ".\exec.py", line 154, in run
 File ".\exec.py", line 48, in __init__
 File ".\subprocess.py", line 633, in __init__
 File ".\subprocess.py", line 842, in _execute_child
 UnicodeEncodeError: 'ascii' codec can't encode characters in position 17-19: ordinal not in range(128)

原因就是脚本所在路径有中文标签,而sublime并未处理这一问题

解决办法为修改控制脚本运行的代码,在sublime的安装目录下:

Sublime Text 2.0.2\Data\Packages\Default\exec.py

文件中的import之后加入以下代码:

reload(sys);
sys.setdefaultencoding('gbk');

即可修复该问题

这里的GBK是中文Windows下的默认编码,如果是其他系统则需要进行对应修改

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.