8

When running my python3 script from Sublime Text 2, the following error occures:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

Furthermore, when running the same script from the terminal, the problem doesn't appear.

The build system settings for Sublime is the following:

{
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"encoding": "utf8",
"path": "/Library/Frameworks/Python.framework/Versions/3.3/bin/"
}

Thanks in advance!

Martin
  • 1,078
  • 1
  • 14
  • 18

2 Answers2

28

After some investigation and research, I figured out what the problem is:

Missing LANG env variable in the subprocess, ran by Sublime Text 2. I fixed it by just adding the LANG variable in my build settings like so:

{
"cmd": ["python3", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"LANG": "en_US.UTF-8"}
}
Martin
  • 1,078
  • 1
  • 14
  • 18
  • 1
    So, I was just about to post this as question again just to provide the answer, but I guess I'll just comment here. I tried this, and it didn't work. Maybe, cause I'm working on a mac. What did the trick for me is not setting the LANG env variable, but the LC_CTYPE to "UTF-8". I literally searched 3 hours, left tooth marks in my table top and thus why maybe this helps. – Ekkstein Oct 23 '15 at 14:45
1

Martin's solutions worked great! In Windows/Python3.5 environment just instead of "LANG" set the following:

"env": {"PYTHONIOENCODING": "utf8"}
tjk
  • 339
  • 3
  • 8