python

sys.stdout and sys.stderr have their encodings determined at runtime. Typically (especially in modern versions of python) they’re UTF-8, but it can depend on the system’s locale and end-user console settings too.

If you have UnicodeEncodeErrors when trying to print UTF-8 strings, it may be because the stream encoding is not UTF-8.

In py3.7 and newer, this can be fixed with `

sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")

But, in 3.6 and earlier, reconfigre doesn’t exist.

See also:

sys.stdout = open(os.dup(sys.stdout.fileno()), mode='w', encoding='utf-8', newline=None, buffering=1)