Skip to content
Advertisement

Why the encodings from sys.stdout.encoding show those two different results as following?

I have two code files which are “run_python.php” and “script.py”.

run_python.pyp

<html>
<?php
    $out = `python script.py`;
    $out = iconv('cp949', 'utf-8', $out);
    echo $out;
?>
</html>

script.py

import locale
import sys

print(locale.getdefaultlocale())
print(locale.getpreferredencoding())
print(sys.stdout.encoding)

When I run run_python.php, the output result on the browser is :

('ko_KR', 'cp949') cp949 cp949

When I run the script.py through the cmd(“python script.py”), the output result on the cmd is :

('ko_KR', 'cp949')
cp949
utf-8

Why the encodings from sys.stdout.encoding show those two different results?

I’m guessing that they’re running on two different shells and the stdout encodings show those two different results? Is that correct? If that’s right or wrong, how do you determine/verify that? It should be helpful if I could determine which shell I’m using on this testing envorinment which is Windows. I’ve searched for a long time, but only could I find was only on Linux.

Testing Environment:

System default locale : CP949
Console codepage : CP949
OS : Windows

Advertisement

Answer

When you run the Python script by itself, sys.stdout is connected to the terminal. When it’s connected to a terminal, the default encoding is determined from the LC_CTYPE or LC_ALL environment variables.

When you run it from PHP, sys.stdout is connected to a pipe. In this case, it defaults to 7-bit ASCII.

See Encoding of Python stdout

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement