在 SublimeText 中直接运行 Python 脚本,出现以下报错提示:
Running python -u C:\Documents and Settings\Administrator\桌面\furl.py
Traceback (most recent call last):
File ".\sublime_plugin.py", line 340, in run_
return self.run(**args)
File ".\exec.py", line 157, in run
File ".\exec.py", line 51, 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 51-52: ordinal not in range(128)
报错原因为当前 py 脚本位于包含中文字符串的路径下,解决方法为修改 exec.py 文件内容,在47行之前添加:
proc_env = os.environ.copy()
proc_env.update(env)
for index, arg in enumerate(arg_list[:]): # added by yzh
arg_list[index] = arg.encode(sys.getfilesystemencoding()) # added by yzh 不增加这两行,在中文字符路径下会出现编码错误。
for k, v in proc_env.iteritems():
proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())
exec.py位于sublime text 2的数据路径下:C:\Documents and Settings\Administrator\Application Data\Sublime Text 2\Packages\Default
subprocess.py位于python安装目录的lib下。
参考:http://www.sublimetext.com/forum/viewtopic.php?f=3&t=12185