Issue
I was debugging a Python 3 script and every time I tried to print a variable, it gave me the following error:
ipdb> inputs
*** UnicodeEncodeError: 'ascii' codec can't encode character '\u22f1' in
position 314: ordinal not in range(12)
I tried to set the default encoding to UTF-8 using sys.setdefaultencoding() and adding # -*- coding: utf-8 -*- at the top of the script, but both didn't work.
Solution
TL;DR: export LANG=C.UTF-8
@mike explains that it happens because Python picks the encoding setting from the environment it's been initiated from. If it can't find a proper encoding, it falls back to its default, 'ascii'.
My solution was to change the locale as described in this answer. Thus, I tried to set export LANG=en_US.UTF-8 and, to my surprise, it didn't work. However, when I tried another locale, export LANG=C.UTF-8 as suggested by another answer, it worked.
Answered By - Yamaneko Answer Checked By - Gilberto Lyons (Python.Easybugfix Admin)
Post a Comment
Post a Comment