In Sublime Text 2 and 3, the console output doesn\'t show the lines with accents on it:
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}\"",
}
]
}
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
See below thread for discussion on a similar issue
Python 2.7 build on Sublime Text 3 doesn't print the '\uFFFD' character
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
How to set sys.stdout encoding in Python 3?
Alternative to execfile in Python 3?