Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As can be seen from screenshot, same script run from Terminal and Gedit behaves differently - in terminal script executes succesfully and in Gedit it raises error:

enter image description here

But if I change python in Gedit tool manager to PYTHONIOENCODING=utf-8 python, then script will execute fine also from Gedit

Why is this, and what setting in Terminal allows python "unicode" code to execute without issues?

Just to update as I added bounty: It's not just GEdit, but also in SciTE error is raised, but not in Geany as Geany executes Python scripts through terminal

share|improve this question
you might want to read this here:… and – xubuntix Apr 3 '12 at 14:44
Good read, thanks. So this issue can be treated as piping problem to Python interpreter for whatever reason, even thou encoding is declared in source file. I feel so sad that most friendly language in the world (IMHO) crawls to this Babylonian Unicode issues in so many ways, and it's so difficult to understand all possible implications, w/o need to be bother "decode early - encode later" - it's so un-pythonic (IMHO) – zetah Apr 3 '12 at 18:59
I feel the same way... But there is hope: as soon as enough libraries have switched to python3, we can actuallystart using it and everything will be alright :-) – xubuntix Apr 4 '12 at 6:27
up vote 3 down vote accepted

Terminal allows python "unicode" code to execute without issues because it has sys.stdout.encoding correctly set.

I change the script as below:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import sys

print sys.stdout.encoding
print u"富强"

Run in Gedit, the output is: None for sys.stdout.encoding.

Run in Terminal, the output is UTF-8 for sys.stdout.encoding and the string is printed correctly.

But if I change locale LC_CTYPE from en_US.UTF-8 to C, run it in Terminal with LC_CTYPE=C ./, I get the following:

Traceback (most recent call last):
  File "./", line 7, in <module>
    print u"富强"
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

sys.stdout.encoding is set to ASCII so it cannot handle the string.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.