python程序无法在sublime text 3中执行,但是在bash中成功执行
问题内容:
这个问题已经在这里有了答案 :
使用python3构建的Sublime Text 2编码错误 (2个答案)
4年前关闭。
#-*- encoding:utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
text = "我们的世界充满了未知数。" # Chinese
print( type(text) ) # unicode
print(text.encode('utf-8'))
print(text) # an error occurs in sublime
python的版本是2.7.6。操作系统是Linux mint 17. $LANG
,bash是en_US.UTF-8
。在崇高的文字中,Ctrl + B
用于运行此玩具程序。输出为:
Traceback (most recent call last):
<type 'unicode'>
我们的世界充满了未知数。
File "~/test.py", line 9, in <module>
print(text) # an error occurs
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: ordinal not in range(128)
[Finished in 0.0s with exit code 1]
[shell_cmd: python -u "~/test.py"]
要么
Traceback (most recent call last):
File "~/test.py", line 9, in <module>
<type 'unicode'>
我们的世界充满了未知数。
print(text) # an error occurs
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: ordinal not in range(128)
[Finished in 0.0s with exit code 1]
[shell_cmd: python -u "~/test.py"]
用bash,eigherpython test.py
或python -u test.py
正确运行:
$ python test.py
<type 'unicode'>
我们的世界充满了未知数。
我们的世界充满了未知数。
$ python -u test.py
<type 'unicode'>
我们的世界充满了未知数。
我们的世界充满了未知数。
这真的让我感到奇怪。 有什么方法可以正确地以崇高的文字运行程序吗?
- 崇高和bash的环境之间有什么区别吗?
- 为什么崇高文本中的输出是乱序的?
问题答案:
Sublime有配置问题。ascii
当无法确定终端编码时,Python使用默认编解码器。它正在正确地找出它,bash
以便工作。
如果set PYTHONIOENCODING=utf8
在启动sublime之前设置了环境变量,则可以强制Python在打印时使用该编码。我对Sublime不熟悉,因此无法建议如何修复其配置。