Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
1  
you might want to read this here: stackoverflow.com/questions/492483/… and farmdev.com/talks/unicode –  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

1 Answer 1

up vote 3 down vote accepted
+50

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 ./test.py, I get the following:

ANSI_X3.4-1968
Traceback (most recent call last):
  File "./test.py", 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

 
discard

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.