Some of my application's libraries are depending on being able to print UTF-8 characters to stdout and stderr. Therefore this must not fail:
print('\u2122')
On my local machine it works, but on my remote server it raises UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in position 0: ordinal not in range(128)
I tried $ PYTHONIOENCODING=utf8 with no apparent effect.
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
works for a while, then stalls and finally fails with ValueError: underlying buffer has been detached
sys.getdefaultencoding() returns 'utf-8', and sys.stdout.encoding returns 'ANSI_X3.4-1968'
What can I do? I don't want to edit third-party libraries.
PYTHONIOENCODING=utf8won't work unless youexportit (or prefix the Python launch with it). Otherwise, it's a local variable inbashthat isn't inherited in the environment of child processes.export PYTHONIOENCODING=utf-8would both set and export it inbash.systo regain access tosetdefaultencodingcan cause problems, and in any event, the correct solution on modern Python (>=3.3) is to make sure your system is using a broadly useful full Unicode supporting default encoding globally. Anything else means you're using hacks to output characters the OS officially doesn't even recognize, and dependent on it playing along despite it claiming it won't work.