一个 python 程序在 sublime text 3 中执行失败,但在 bash 中执行成功
A python program fails to execute in sublime text 3, but success in bash
#-*- 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。 OS 是 Linux mint 17。bash 中的 $LANG
是 en_US.UTF-8
。在sublime text中,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中,有一个python test.py
或python -u test.py
运行正确:
$ python test.py
<type 'unicode'>
我们的世界充满了未知数。
我们的世界充满了未知数。
$ python -u test.py
<type 'unicode'>
我们的世界充满了未知数。
我们的世界充满了未知数。
真是让我想不通。 有什么方法可以正确运行 sublime text 中的程序吗?
- sublime的环境和bash有什么区别吗?
- 为什么sublime text中的输出是乱序的?
Sublime 有配置问题。 Python 在无法确定终端编码时使用默认的 ascii
编解码器。它在 bash
中正确计算出来,所以它有效。
如果您在启动 sublime 之前设置环境变量 set PYTHONIOENCODING=utf8
,您可以强制 Python 在打印时使用该编码。我不熟悉 sublime,所以我不能建议如何修复它的配置。
#-*- 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。 OS 是 Linux mint 17。bash 中的 $LANG
是 en_US.UTF-8
。在sublime text中,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中,有一个python test.py
或python -u test.py
运行正确:
$ python test.py
<type 'unicode'>
我们的世界充满了未知数。
我们的世界充满了未知数。
$ python -u test.py
<type 'unicode'>
我们的世界充满了未知数。
我们的世界充满了未知数。
真是让我想不通。 有什么方法可以正确运行 sublime text 中的程序吗?
- sublime的环境和bash有什么区别吗?
- 为什么sublime text中的输出是乱序的?
Sublime 有配置问题。 Python 在无法确定终端编码时使用默认的 ascii
编解码器。它在 bash
中正确计算出来,所以它有效。
如果您在启动 sublime 之前设置环境变量 set PYTHONIOENCODING=utf8
,您可以强制 Python 在打印时使用该编码。我不熟悉 sublime,所以我不能建议如何修复它的配置。