Printing utf-8 strings in Sublime Text 2 console with Windows 7

When you run this code python myscript.pyfrom the Windows console cmd.exe (i.e., outside of Sublime Text), it works:

# coding: utf8
import json
d = json.loads("""{"mykey": {"readme": "Café"}}""")
print d['mykey']['readme']

a cafe

When launched inside Sublime Text 2 with CTRL+Bit does not work:

  • Similar to this (default):

    print d ['mykey'] ['readme']
    UnicodeEncodeError: codec 'ascii' cannot encode u '\ xe9' at position 3: serial number is not in range (128)
    [Finished at 0.1 with exit code 1]

  • or like this, after applying the solution from this answer print UTF-8 in Python 3 using Sublime Text 3 (i.e. adding "env": {"PYTHONIOENCODING": "utf8"},to the build system):

    [ - utf-8]
    [ - utf-8]
    [ 0,1 ]

  • "encoding": "utf-8" Python Sublime-build

print Sublime Text 2 ( Windows), UTF8 char?

: UTF-8 Python 3 Sublime Text 3, .

Python.sublime-build:

{ "cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"variants": [ { "name": "Run", "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "cmd": ["C:\\Python27-64\\python.exe", "-u", "$file"] } ] }

( "env": ..., "encoding": ...)

+5
3

, , TL; DR , ​​Sublime Text 2 ( , exec).

Sublime, ( , ), Sublime Text 3 , Sublime 3 exec .


-, , , :

[ - utf-8]

Sublime, , Python. , , ( / ) env, , Python UTF-8 , , .


Sublime Text 2 Python 2.7.14 Windows 7. Python 3 PATH, C:\Python27-64, , .

PackageResourceViewer , Sublime .

script , , :

# coding: utf8
import sys

print(sys.version)
print("Café")

, Tools > Build System ​​ Automatic, Ctrl+B :

3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]
[Decode error - output not utf-8]
[Finished in 0.1s]

, , , Python 3 , Python 2 , Python 3.

Python.sublime-build:

{
    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

PackgeResourceViewer, , Python 2:

{
    "cmd": ["C:\\Python27-64\\python.exe", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

:

2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)]
Café
[Finished in 0.1s]

, Python 2, , .

, , , , , . , sys.version:

# coding: utf8
import sys

#print(sys.version)
print("Café")

:

[Decode error - output not utf-8]
[Decode error - output not utf-8]
[Finished in 0.1s]

, , , :

# coding: utf8
import sys

# print(sys.version)
print("Café au lait")

, :

Café au lait
[Finished in 0.1s]

, exec, Sublime Text Default. , ​​ , , , .

, Sublime Text 3 exec, ( ) , , .

exec Sublime 2, , , , .

public gist, exec.py, exec, .

, ( ) , , , env , Python UTF-8, .

+4

:

# coding: utf8
import json
d = json.loads("""{"mykey": {"readme": "Café"}}""", encoding='latin1')
print d['mykey']['readme'].encode('latin1')
+1

: encoding Python.sublime-build:

{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "cp1252",
...

: "encoding": "latin1", , , - , - "encoding": "utf8" , .py - UTF8, Python 3 UTF8 .. Mystery!

0

All Articles