python程序在sublime text 3中执行失败,但在bash中成功
python的版本是2.7.6。操作系统是LinuxMint17<代码>$LANG在bash中是python程序在sublime text 3中执行失败,但在bash中成功,python,bash,sublimetext3,Python,Bash,Sublimetext3,python的版本是2.7.6。操作系统是LinuxMint17$LANG在bash中是en_US.UTF-8。在升华文本中,Ctrl+B用于运行此玩具程序。输出为: #-*- encoding:utf-8 -*- from __future__ import (absolute_import, division, print_function, unicode_literals) text = "我们的世界充满了未知数。" # Chinese
en_US.UTF-8
。在升华文本中,Ctrl+B用于运行此玩具程序。输出为:
#-*- 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
这真让我好奇。有什么方法可以正确运行升华文本中的程序吗
- sublime和bash的环境有什么不同吗李>
- 为什么升华文本中的输出出现故障
- 升华存在配置问题。Python在无法确定终端编码时使用默认的
ascii
编解码器。它在bash
中正确地计算出来,因此它可以工作
如果在启动sublime之前设置环境变量
set PYTHONIOENCODING=utf8
,则可以强制Python在打印时使用该编码。我不熟悉sublime,因此无法建议如何修复其配置。您已经正确运行了它-您只需使用.encode()
@MattDMo我前面的问题不准确。我已经更新了。在SublimiteText下,您可能会看到一些随机交错的文本打印到stdout
和stderr
。bash是否将同一字符串打印两次?@roeland是的。我在问题中添加了输出,我猜sublime在运行代码时为python提供了错误的语言环境。你试过在他们的论坛上寻求帮助吗?
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"]
$ python test.py
<type 'unicode'>
我们的世界充满了未知数。
我们的世界充满了未知数。
$ python -u test.py
<type 'unicode'>
我们的世界充满了未知数。
我们的世界充满了未知数。