Why does termcolor output control characters instead of colored text in the Windows console?

user3170921 picture user3170921 · Feb 18, 2014 · Viewed 20.1k times · Source

I just installed termcolor for Python 2.7 on Windows. When I try to print colored text, I get the color codes instead.

from termcolor import colored
print colored('Text text text', 'red')

Here is the result:

Screenshot of the Windows console window with the line: "←31mText text text←[0m"

I obtain the same results on Far Manager and when I tried to run the script as a standalone application.

Answer

Joachim Isaksson picture Joachim Isaksson · Feb 18, 2014

To make the ANSI colors used in termcolor work with the windows terminal, you'll need to also import/init colorama;

>>> from termcolor import *
>>> cprint('hello', 'red')
←[31mhello←[0m
>>> import colorama
>>> colorama.init()
>>> cprint('hello', 'red')
hello                                    <-- in red color
>>>