11

In Sublime Text 2 and 3, the console output doesn't show the lines with accents on it:

Example  

I'm using Tools > Build in vanilla Sublime in Windows with automatic Build System to execute it.

Is there any fix to this?

Enteleform
  • 3,753
  • 1
  • 15
  • 30
jcrs
  • 449
  • 4
  • 11
  • What version of Python are you using? – MattDMo May 11 '16 at 01:53
  • 1
    Python 3, but the problem is the same with Python 2 however – jcrs May 11 '16 at 12:00
  • @Basj the example in this question seems to work fine out of the box in the latest Sublime with the script presented and Python 3; are you saying that this is still an issue for you as presented here? – OdatNurd May 08 '18 at 22:06
  • @OdatNurd I still use (the latest version of) Python 2.7 for some projects. – Basj May 08 '18 at 22:14
  • @Basj Hmm.. it seems to work for me there as well (or with 2.7.14 anyway); are you on Sublime 3.1? Perhaps you have a modified version of the `Default/exec.py` plugin that I helped you with previously? – OdatNurd May 08 '18 at 22:27
  • @jcrs It seems fixed with the last Sublime Text version (see my answer). – Basj Nov 25 '19 at 18:02

3 Answers3

3

Set the encoding of standard system output in your document to UTF-8:

import sys
import codecs

sys.stdout = codecs.getwriter( "utf-8" )( sys.stdout.detach() )

print( "1" )
print( "áéíóúý âêîôû äëïöü àèìòù ãñõ" )
print( "2" )

To automatically apply UTF-8 encoded output to all documents, implement the previous method as an inline command within your Python.sublime-build file.

After the encoding has been set, your document is loaded via exec within the inline command.

{
    "cmd": [ "python", "-u", "-c", "import sys; import codecs; sys.stdout = codecs.getwriter( 'utf-8' )( sys.stdout.detach() ); exec( compile( open( r'$file', 'rb' ).read(), r'$file', 'exec'), globals(), locals() )" ],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "python -m py_compile \"${file}\"",
        }
    ]
}

Tip: Use PackageResourceViewer to create a user copy of Python.sublime-build


Tested with Sublime Text 3 ( Stable Channel, Build 3103 ) and Python 3.4.3


Sources:

How to set sys.stdout encoding in Python 3?

Alternative to execfile in Python 3?

Community
  • 1
  • 1
Enteleform
  • 3,753
  • 1
  • 15
  • 30
  • Thanks for your answer, but since printing accents works on other consoles, I'm wondering if there is a fix at Sublime side, to avoid adding this code to every single project. – jcrs May 10 '16 at 13:40
  • 1
    @jcrs: I just updated the answer with a build method. – Enteleform May 10 '16 at 19:14
  • It's a bit hackish to have to add this each time we do a new Python script involving utf8 characters. Strangely, this problem is not present in SublimeText 2. – Basj Feb 25 '18 at 17:33
  • 2
    @Basj Definitely a hack, but it does not have to be added for each Python script. If you revisit the 2nd section of the answer (after "To automatically apply.."), you'll see that the hack boilerplate is incorporated into a build system. Meaning that all scripts run with that build system will work with proper UTF-8 encoded output. – Enteleform Feb 25 '18 at 23:42
2

The cleaner solution is to specify the encoding as a part of the build settings

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

This works in most cases. But in certain cases you may need to remove the -u which is basically to stop unbuffered output, as it may cause issues

Encoding

See below thread for discussion on a similar issue

Python 2.7 build on Sublime Text 3 doesn't print the '\uFFFD' character

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
0

It seems fixed with Sublime Text 3.2.2 build 3211: when I open test.py containing:

print("1")
print("á")
print("2")

then when building with CTRL+B (using Python 3.6), then it works normally, out-of-the-box.


Remark: the default Python.sublime-build is indeed now:

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

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "python -m py_compile \"${file}\"",
        }
    ]
}
Basj
  • 41,386
  • 99
  • 383
  • 673