Search code examples
pythonpython-3.xcolorama

Print cmd with colors


I am trying to get colored command line output. I was able to get colored Python console output using colorama with this:

from colorama import Fore
from colorama import Style

print(f'{Fore.GREEN}A')
print(f'{Fore.RED}B')
print('C')
print(f'{Style.RESET_ALL}D')
print('E')

This perfectly works inside the Python Console in PyCharm. However, if I run the program under Windows cmd. There is no color at all but the colorama text is just added without any effect:

←[32mA
←[31mB
C
←[0mD
E

Can I modify the code so that it also works in Windows cmd?


Solution

  • You'll need to add convert=True to your colorama init call:

    from colorama import Fore, Style, init
    
    init(convert=True)
    
    print(f'{Fore.GREEN}A')
    print(f'{Fore.RED}B')
    print('C')
    print(f'{Style.RESET_ALL}D')
    print('E')