4

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.

Cerin
  • 60,957
  • 96
  • 316
  • 522

1 Answers1

4

Try setting the PYTHONIOENCODING variable, or using the solutions in this question.

Community
  • 1
  • 1
aghast
  • 14,785
  • 3
  • 24
  • 56