Python程序未能在崇高的文本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
蟒蛇的版本是2.7.6。该操作系统是Linux Mint的17 $ LANG
在bash是的en_US.UTF-8
。在崇高的文本,<大骨节病>按Ctrl + B 骨节病>用来运行这个玩具程序。输出是:
The version of python is 2.7.6. The OS is Linux mint 17. $LANG
in bash is en_US.UTF-8
. In sublime text, Ctrl + B is used to run this toy program. The output is:
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中,非此即彼蟒蛇test.py
或蟒蛇-u test.py
运行正常:
In bash, eigherpython test.py
or python -u test.py
runs correctly:
$ python test.py
<type 'unicode'>
我们的世界充满了未知数。
我们的世界充满了未知数。
$ python -u test.py
<type 'unicode'>
我们的世界充满了未知数。
我们的世界充满了未知数。
这真的让我惊叹。 是否有运行在崇高的文本程序的方式是否正确?
- 有崇高和bash环境之间有什么区别?
- 为什么崇高的文本输出是乱序?
推荐答案
崇高配置有问题。 Python使用默认的 ASCII
codeC时,它不能确定终端编码。它在庆典
正确地计算出来,以便它的作品。
Sublime has a configuration problem. Python uses the default ascii
codec when it can't determine the terminal encoding. It is figuring it out correctly in bash
so it works.
如果您设置环境变量设置PYTHONIOENCODING = UTF8
启动崇高您可以强制Python的打印时使用的编码前。我不熟悉崇高的,所以我不能建议如何解决它的配置。
If you set the environment variable set PYTHONIOENCODING=utf8
before launching sublime you can force Python to use that encoding when printing. I'm not familiar with sublime so I can't suggest how to fix its configuration.
这篇关于Python程序未能在崇高的文本3执行,但在bash成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!