Python 3 throws UnicodeEncodeError with environment variable LANG=C
<p>I have the following Python script:</p>

<pre><code>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

print('âº')
</code></pre>

<p>When I run it on my Debian system, it produces the following output, as expected:</p>

<pre><code>$ ./test.py 
âº
$ 
</code></pre>

<p>However, when I change locale to "C", by setting the <code>LANG</code> environment variable, the script throws a <code>UnicodeEncodeError</code>:</p>

<pre><code>$ LANG=C ./test.py
Traceback (most recent call last):
  File "./test.py", line 4, in &lt;module&gt;
    print('\u263a')
UnicodeEncodeError: 'ascii' codec can't encode character '\u263a' in position 0: ordinal not in range(128)
$ 
</code></pre>

<p>This problem prevents this script from being executed in minimal environments, such as during boot or in embedded systems. Also, I suspect that many existing Python programs can be broken by executing them with <code>LANG=C</code>. <a href="http://stackoverflow.com/questions/24095382/python-3-4-causes-unicodeencodeerror-on-apache2-server-mac-but-works-fine-in-c">Here's an example</a> on Stackoverflow of a program that presumably broke because it's executed in the "C"-locale. </p>

<p>Is this a bug in Python? What's the best way to prevent this?</p>

<p>This is because Python 3 uses the locale settings to deduce the output character encoding; that is, Python will use the locale that would be displayed for <code>LC_CTYPE</code> when you execute the locale command:</p>

<pre><code>% locale 
...
LC_CTYPE="en_US.UTF-8"
...
</code></pre>

<p>If you force <code>LC_CTYPE</code> to <code>C</code>, then Python will assume that ASCII should be used as the output encoding. And ASCII doesn't have a mapping for <code>U+263A</code>.</p>

<p>If you want Python to know how to encode Unicode properly, set the <code>LC_CTYPE</code> to an appropriate value, or write binary to fd 1.</p>

python python-3.x unicode locale