python encoding error only when called as external process python encoding error only when called as external process bash bash

python encoding error only when called as external process


The problem here is that in the second call you are basically writing to a pipe that only accepts bytestrings (file-like object). The same happens if you try to execute this:

python x.py > my_fileTraceback (most recent call last):File "x.py", line 2, in <module>    print xUnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 3: ordinal not in range(128)

As the receiver only understands bytestrings and not unicode characters you must first encode the unicode string into a bytestring using the encode function:

x = u'Gen\xe8ve'.encode('utf-8') print x

This will print the the unicode string encoded as a utf-8 bytestring (a sequence of bytes), allowing it to be written to a file-like object.

$echo $(python x.py)Genève$python x.py Genève


As you suspect, Python doesn't know how to print unicode when its standard output is not a known terminal. Consider encoding the string before printing it:

# coding: utf-8x = u'Gen\xe8ve'print x.encode("utf-8")

Note that the invoking program and your script will need to agree in a common encoding.