How `[System.Console]::OutputEncoding/InputEncoding` with Python?
<p>Under Powershell v5, Windows 8.1, Python 3. Why these fails and how to fix?</p>

<pre class="lang-bsh prettyprint-override"><code>[system.console]::InputEncoding = [System.Text.Encoding]::UTF8; 
[system.console]::OutputEncoding = [System.Text.Encoding]::UTF8; 
chcp; 
"import sys
print(sys.stdout.encoding)
print(sys.stdin.encoding)
sys.stdout.write(sys.stdin.readline())
" | 
sc test.py -Encoding utf8; 
[char]0x0422+[char]0x0415+[char]0x0421+[char]0x0422+"`n" | py -3 test.py
</code></pre>

<p>prints:</p>

<pre><code>Active code page: 65001
cp65001
cp1251
Ð¿Â»Ñ????
</code></pre>

<p>You are piping data into Python; at that point Python's <code>stdin</code> is no longer attached to a TTY (your console) and won't guess at what the encoding might be. Instead, the default system locale is used; on your system that's cp1251 (the Windows Latin-1-based codepage).</p>

<p>Set the <a href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONIOENCODING"><code>PYTHONIOENCODING</code> environment variable</a> to override:</p>

<blockquote>
  <p><code>PYTHONIOENCODING</code><br>
  If this is set before running the interpreter, it overrides the encoding used for stdin/stdout/stderr, in the syntax <code>encodingname:errorhandler</code>. Both the <code>encodingname</code> and the <code>:errorhandler</code> parts are optional and have the same meaning as in <code>str.encode()</code>.</p>
</blockquote>

<p>PowerShell doesn't appear to support per-command-line environment variables the way UNIX shells do; the easiest is to just set the variable first:</p>

<pre><code>Set-Item Env:PYTHONIOENCODING "UTF-8"
</code></pre>

<p>or even</p>

<pre><code>Set-Item Env:PYTHONIOENCODING "cp65001"
</code></pre>

<p>as the Windows UTF-8 codepage is apparently not <em>quite</em> UTF-8 really, depending on the Windows version and on wether or not pipe redirection is used.</p>

python .net shell powershell