-1

I just installed termcolor for python 2.7 on windows8.1. When I try to print colored text, I get the strange output.

from termcolor import colored
print colored('Hello world','red')

Here is the result:

[31mHello world[0m

Help to get out from this problem.Thanks,In advance

WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
vijay Dave
  • 51
  • 1
  • 5
  • Possible duplicate of [Why does termcolor output control characters instead of colored text in the Windows console?](https://stackoverflow.com/questions/21858567/why-does-termcolor-output-control-characters-instead-of-colored-text-in-the-wind) – Jongware Apr 23 '18 at 09:59

2 Answers2

2

See this stackOverflow post.

It basically says that in order to get the escape sequences working in Windows, you need to run os.system('color') first.

For example:

import termcolor
import os
os.system('color')
print(termcolor.colored("Stack Overflow", "green")
Xiokraze
  • 353
  • 4
  • 14
0

termcolor or colored works perfectly fine under python 2.7 and I can't replicate your error on my Mac/Linux.

If you looks into the source code of colored, it basically print the string in the format as

\033[%dm%s\033[0m' % (COLORS[color], text)

Somehow your terminal environment does not recognise the non-printing escape sequences that is used in the unix/linux system for setting the foreground color of xterm.

hcheung
  • 3,377
  • 3
  • 11
  • 23