Python main.py error while using Git bash

$ python main.py
Using Poland server backend.
Traceback (most recent call last):
File “C:\Users\Tom\RedditVideoMakerBot\main.py”, line 22, in
print(
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\encodings\cp1250.py”, line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: ‘charmap’ codec can’t encode characters in position 2-8: character maps to

By Thomas via Discussions on Python.org at 17Jul2022 16:51:

$ python main.py
Using Poland server backend.
Traceback (most recent call last):
File “C:\Users\Tom\RedditVideoMakerBot\main.py”, line 22, in
print(
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\encodings\cp1250.py”, line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: ‘charmap’ codec can’t encode characters in position 2-8: character maps to

I doubt this has anything to with git or with bash.

It is had to say exactly what is wrong without also seeing your code.
But I would guess that you’re printing a string which contains
characters which are not represented in the cp1250 character set, which
is what I am guessing is part of your output locale.

Can you run your code in an environment accepting UTF-8 output? Or some
other Unicode output?

In line 22 of your main.py, can you print repr(foo) for whatever foo
is causing you trouble?

Cheers,
Cameron Simpson cs@cskk.id.au

2 Likes

Yes, it looks like so. Nowadays it so weird to see an 8-bit character set. It feels like an ancient history :slight_smile:

In a Unix-like environment (which Git bash on Windows is) use the command locale to see your locale settings including the character set. On my system (Windows 10 21H2, Git 2.37.1) I have UTF-8 as expected:

MINGW64 ~
$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_ALL=

By Václav Brožík via Discussions on Python.org at 18Jul2022 07:14:

In a Unix-like environment (which Git bash on Windows is)

Ah, I had not realised that “git bash” was a thing.

Cheers,
Cameron Simpson cs@cskk.id.au

If Python detects standard I/O as console files, then it uses io._WindowsConsoleIO, which supports the native UTF-16 of the console and provides a UTF-8 raw interface to Python.

“git-bash.exe” uses the mintty (MSYS) terminal instead of the console. Under mintty, standard I/O is set to named-pipe files with particular names that the MSYS runtime reserves for terminal I/O (e.g. pipes named “msys-ffffffffffffffff-pty0-from-master” and “msys-ffffffffffffffff-pty0-to-master”). In the Windows build of Python, they’re accessed as regular pipe files, so standard I/O will be non-interactive and by default use the process ANSI code page. You can force interactive mode and UTF-8 via python -iX utf8. It will still be a bad user experience for the REPL (e.g. no command-line editing or history, and no Ctrl+Z or Ctrl+D to exit). There are builds of Python for MinGW and MSYS that recognize these special pipe files for interactive terminal I/O.

2 Likes