UnicodeEncodeError in python3
<p>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:</p>

<pre><code>print('\u2122')
</code></pre>

<p>On my local machine it works, but on my remote server it raises <code>UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in position 0: ordinal not in range(128)</code></p>

<p>I tried <code>$ PYTHONIOENCODING=utf8</code> with no apparent effect.</p>

<pre><code>sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
</code></pre>

<p>works for a while, then stalls and finally fails with <code>ValueError: underlying buffer has been detached</code></p>

<p><code>sys.getdefaultencoding()</code> returns <code>'utf-8'</code>, and <code>sys.stdout.encoding</code> returns <code>'ANSI_X3.4-1968'</code></p>

<p>What can I do? I don't want to edit third-party libraries.</p>

<p>From @ShadowRanger's comment on my question,</p>

<blockquote>
  <p><code>PYTHONIOENCODING=utf8</code> won't work unless you <code>export</code> it (or prefix the Python launch with it). Otherwise, it's a local variable in <code>bash</code> that isn't inherited in the environment of child processes. <code>export PYTHONIOENCODING=utf-8</code> would both set and export it in <code>bash</code>.</p>
</blockquote>

<p><code>export PYTHONIOENCODING=utf-8</code> did the trick, UTF-8 characters no longer raise UnicodeEncodeError</p>

python python-3.x unicode utf-8 locale