Python print Unicode string via 'Git Bash' gets 'UnicodeEncodeError'

Viewed 1362

in test.py i have

print('Привет мир')

with cmd worked as normal

> python test.py
?????? ???

with Git Bash got error

$ python test.py
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    print('\u041f\u0440\u0438\u0432\u0435\u0442 \u043c\u0438\u0440')
  File "C:\Users\raksa\AppData\Local\Programs\Python\Python36\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: character maps to <undefined>

enter image description here

Does anyone know the reason behind of getting error when execute python code via Git Bash?

4 Answers

Had this problem with python 3.9

import sys, locale
print("encoding", sys.stdout.encoding)
print("local preferred", locale.getpreferredencoding())
print("fs encoding", sys.getfilesystemencoding())

If this returns "cp1252" and not "utf-8" then print() doesn't work with unicode.

This was fixed by changing the windows system locale.

Region settings > Additional settings > Administrative > Change system locale > Beta: Use Unicode UTF-8 for worldwide language support

Since Python 3.7 you can do

import sys
sys.stdout.reconfigure(encoding='utf-8')

This mostly fixes the git bash problem for me with Chinese characters. They still don't print correctly to standard out on the console, but it doesn't crash, and when redirected to a file the correct unicode characters are present.

Credit to sth in this answer.

Set the the environment variable PYTHONUTF8=1, or Use -Xutf8 command line option.

Related