Search code examples
pythonunicodegit-bashpython-unicode

Git-bash not displaying Unicode with logging


I am having trouble displaying Unicode characters on git-bash when working with Python's logging.

  • Without logging - everything works fine.
  • With cmd - everything works fine.
  • With PyCharm - everything works fine.
  • logging with git-bash - not working (displaying "\u2501").

See below...

I am using:

  • Windows 10
  • git version 2.37.2.windows.2
  • Python 3.9.6 (Same issue with Python 3.7).

enter image description here

enter image description here

enter image description here And even this not working:

enter image description here


Solution

  • Well, as commented, logging is using sys.stderr and not sys.stdout.

    As for the terminals (at least on my Windows machine):

    • When running Python from cmd the default encoding for both sys.stdout and sys.stderr is utf-8.
    • When running Python from git-bash the default encoding for both sys.stdout and sys.stderr is cp1252. Really no idea why. I would assume most git-bash users want Linux-like behavior.

    It seems that both terminals accept utf-8.

    Since my script only needs to be run on these two terminals, the following solves my problem:

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