The following code:
print('\N{WAVING BLACK FLAG}')
is as simple as it can be. Yet on some machines it prints the character as expected, on other it raises a UnicodeEncodeError
with a message 'ascii' codec can't encode character '\U0001f3f4' in position 0: ordinal not in range(128)
.
Why printing a character can sometimes lead to UnicodeEncodeError
? There is no mention about any encoding in the documentation. And is there any way how to make sure the string will be printed without raising any exceptions?
I managed to isolate a reproducible example:
import subprocess
import sys
subprocess.run([sys.executable, 'test.py'], env=dict())
The test.py
contains just the single print statement mentioned above. This example raises a UnicodeEncodeError
on all tested machines... but only when tested with Python 3.6
. When tested with Python 3.7
it prints the character as expected.
If otherwise your locale settings are the same for the tests on both 3.6 and 3.7, this may be due to PEP538 coming into effect since python 3.7
PEP538 coerces the C locale to UTF-8.