ANSI color not working for Git Bash on Windows 10 using Windows Terminal

Question:

I’m using Git Bash on Windows 10 with Windows Terminal and for this Python project, the ANSI escape sequence are not working.

from colorama import init, Fore
from sys import stdout

init(convert=True)

# code ...

I tried to print a test text

# The code above
stdout.write('{GREEN}Test{RESET}'.format(GREEN=Fore.GREEN, RESET=Fore.RESET)

Here’s the output:

←[32mTest←[0m

I’m sure that my terminal supports ANSI sequence since I’ve tested it with Bash, TS (Deno) and JS (NodeJS). They all worked. I’ve also tested on Command Prompt and it works fine for Python. Maybe this is a problem with Git Bash executing Python itself?

I’ve also tried writing the hex code directly but still no luck. Check the code below
write.py
Example image

Asked By: Onyxzen

||

Answers:

After wasting some time testing, apparently only using print works

#!/usr/bin/env python3
from colorama import init, Fore
from sys import stdout

# Initialize
init()

# Using `Fore`
print(f'{Fore.GREEN}Test{Fore.RESET}')

# Using actuall hex values
print('x1B[31mTestx1B[0m')

# Using stdout.write
stdout.write(f'{Fore.GREEN}Test{Fore.RESET}n')
stdout.write('x1B[31mTestx1B[0mn')
Test
Test
←[32mTest←[39m
←[31mTest←[0m

Result

EDIT:

Using input will also fails

# This will fail
xyz = input(f'{Fore.RED}Red text{Fore.RESET}')

# Consider using this as an alternative
print(f'{Fore.RED}Red text{Fore.RESET}', end='')
xyz = input()
Answered By: Onyxzen