Search code examples
python-3.xencodingutf-8sublimetext3osx-yosemite

How to change the preferred encoding in Sublime Text 3 for MacOS


I want to change the preferred encoding from US-ASCII to UTF-8 in Sublime Text 3 on Yosemite. The preferred encoding in the bash is set to UTF-8 so when python is run in the terminal:

import locale
print(locale.getpreferredencoding())

the output is: UTF-8 When the same code is run in Sublime Text, the output is US-ASCII.

Setting in the build system for Python 3:

"encoding": "UTF-8"

or

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

has not helped.

How can the setting be changed permanently so that I don't have to call locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') in a script as a fix.


Solution

  • In ST3's build system for Python, you can specify that it should set the LANG environment variable, and this will affect the result returned from locale.getpreferredencoding(), so that you don't need to amend any Python scripts.

    Example:

    "env": {"PYTHONIOENCODING": "utf-8", "LANG": "en_US.UTF-8"},
    

    This has been confirmed to work on Linux as well as MacOS and Windows.