4

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.

саша
  • 521
  • 5
  • 20
  • 1
    from my testing in Linux, it is enough to set the `LANG` environment variable in the build system i.e. `"env": {"PYTHONIOENCODING": "utf-8", "LANG": "en_US.UTF-8"},` - if this works for you on MacOS, I'll make it an answer :) – Keith Hall Feb 08 '17 at 06:45
  • 1
    Thanks, it works for MacOS too. – саша Feb 08 '17 at 12:46

1 Answers1

5

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.

Basj
  • 41,386
  • 99
  • 383
  • 673
Keith Hall
  • 15,362
  • 3
  • 53
  • 71
  • How exactly do you make this change? I can't find any way to edit a built-in build system. Additionally, what happens if you have more than one Python build system installed? Does this apply to all plugins in ST3 (which are written in Python), even if you're editing non-Python files? – Matt Jul 20 '18 at 14:25
  • To add a new build system go to `Tools->Build System->New Build System`. For Python 3 you can use ```{ "shell_cmd": "/usr/bin/env python3 ${file}", "selector": "source.python", "file_regex": "^(...*?):([0-9]*):?([0-9]*)", "working_dir": "${file_path}", "env": {"PYTHONIOENCODING": "utf-8", "LANG": "en_US.UTF-8"} }``` – Robin Keskisarkka Oct 19 '18 at 11:55
  • You can also modify the existing one for Python by going into `~/Library/Application Support/Sublime Text 3/Packages/User/`. The `~/Library` directory is hidden in Finder by default so you may have to use `Cmd + Shift + . (dot)` to show/hide it (or just navigate there using the terminal). – Robin Keskisarkka Oct 19 '18 at 12:07