Cerin
Cerin

Reputation: 64880

How to fix Python encoding in terminal

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.

Upvotes: 4

Views: 4866

Answers (1)

aghast
aghast

Reputation: 15310

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

Upvotes: 4

Related Questions