How do you set the default encoding Python uses with sys.stdout when run from a terminal program like Gnome-Terminal?
If I execute a Python script inside Gnome-Terminal, I get the notorious "UnicodeEncodeError: 'ascii' codec can't encode character" error if the script tries to print unicode characters. However, even though Gnome-Terminal's encoding is set to "UTF-8", if I print sys.stdout.encoding it shows "ANSI_X3.4-1968".
Minimal code to reproduce the error is:
python -c 'import sys; print sys.stdout.encoding; print u"\u0411\n"' Why isn't Python using the terminal's encoding of utf-8?
Note, I'm not asking how to convert my string to ASCII.
echo $LANGandecho $LC_ALLprint?# -*- coding: utf-8 -*-at the top of your script file ?localecommand in your terminal, please?# coding: utf8declares the encoding of the source file and nothing else.