I’m using Sublime Text version 3.1.1, Python 3.6.5, MacOS 10.13.4
I want to be able to use German special characters such as “ä, ö, ü, ß” in my Strings. How am I able to do this?
Thanks for any help!
I’m using Sublime Text version 3.1.1, Python 3.6.5, MacOS 10.13.4
I want to be able to use German special characters such as “ä, ö, ü, ß” in my Strings. How am I able to do this?
Thanks for any help!
UnicodeEncodeError: ‘ascii’ codec can’t encode character ‘\xe4’ in position 0: ordinal not in range(128)
python 3 should handle all code as utf-8 by default. Maybe some environment variable forcing something else?
You can force it by adding the following code to the first line.
# -*- coding: utf-8 -*-
var1 = “ä”
print(var1)
(this very small program doesn’t work)
UnicodeEncodeError: ‘ascii’ codec can’t encode character ‘\xe4’ in position 0: ordinal not in range(128)
Yes, if I run it from the terminal, it works. If I run it from within SublimeText is doesn’t. (Build System file is set to Python3)
Can you show the contents of your build system? Your problem sounds like perhaps the appropriate environment variable isn’t being set or something along those lines,
That’s not the build system, that’s the output of the build. To better see what it’s trying to do, can you include the contents of Python3.sublime-build
?
I would try something like the following modifications to your build above and see if that works better for you; possibly the Python interpreter does something different in your shell than it does if you run it outside of the shell.
{
"shell_cmd": "/usr/local/bin/python3 -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
}
The PYTHONIOENCODING
was what I had in mind, too. But I am a bit surprised about this behavior as it is said python3 to use utf-8 as encoding by default. Actually it does on Windows without setting this environment variable to be present. Strange that it ignores the utf-8 cookie, too.
Maybe a limitation of the MacOS version of python?
I’m in no way a Python expert, but it’s my understanding that the coding:
style comment is meant to tell the Python interpreter what the encoding of the Python script being executed is, not what the encoding used for I/O is.
I think the default is already UTF-8 for that case in Python 3 so the comment is mostly either for if your code needs to work with Python 2 as well, if you’re changing the default, or if you’re using an editor that can use it as a hint for what encoding to use when you edit the file.
When it comes to the encoding used for I/O, I think it tries to determine what encoding to use by default based on where the interpreter is executing, and if it can’t do that it falls back to ascii
as the default, unless the PYTHONIOENCODING
variable is set, in which case it just uses that instead of trying to figure it out for itself.
As such it may be something related to how the interpreter gets executed on MacOS that stops it from being able to properly determine the encoding. I’d buy that just based on how on MacOS GUI applications get launched in a way that gives them a different overall environment than the Terminal.
My MacBook displays the same behaviour as above; just a bare cmd
or shell_cmd
seems to make it default to ascii
.
I was just faced to that issue, too, while back porting some stuff to Sublime Text 2.
The code causing that issue was pretty much the same as from the initial post.
# -*- coding: utf-8 -*-
from .text_utils import format_blame_message
# the default template to use if jinja2 is unavailable or failed
DEFAULT_TEMPLATE = '⟢ {author} ({author_ago}) · {summary}'
Without the # -*- coding: utf-8 -*-
the module could not even be loaded. Yes you are right, with it to be required for the interpreter to handle the source containing none-ascii well.
Even with that cookie, Sublime Text 2 (python 2.6) fails execution with:
Traceback (most recent call last):
...
File ".\modules\blame\status_text.py", line 38, in render
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
To solve the issue, the static text needs to be marked as unicode by u'...'
.
# the default template to use if jinja2 is unavailable or failed
DEFAULT_TEMPLATE = u'⟢ {author} ({author_ago}) · {summary}'
P.S.: Same issue whith adding an ä
to that string.