0

I'm just working on a fun project to produce color in the console. In the Windows Terminal, it is working fine. However, in CMD it is not. Could you please help me?

from termcolor import cprint

cprint("Hello World","blue")

input()

Windows Terminal:

2

Command Prompt:

3

Thank you.

Yevgeniy Kosmak
  • 3,561
  • 2
  • 10
  • 26
lunix
  • 151
  • 1
  • 14
  • Please post text as text, not an image of text. – Scott Hunter Dec 25 '21 at 13:37
  • Updated, please help. – lunix Dec 25 '21 at 13:41
  • I recommend you not to use such program (and most naive libraries to give you colours): it just do not check terminal capabilities, and it has hard coded escape sequences (without knowing if the terminal has any idea of ECMA-48). There are various libraries which do good job (so portable and no naive guesses). – Giacomo Catenazzi Dec 25 '21 at 19:01

3 Answers3

1

Using Colorama to make Termcolor work in Windows:

This is where I got the reference from :REFERENCE

from colorama import init
from termcolor import cprint

init()
#colored() will also work
cprint("Hello World test","cyan")

#hit enter to exit

input()


lunix
  • 151
  • 1
  • 14
0

Try to fix it by installing colorama package:

pip install colorama

Source.

Yevgeniy Kosmak
  • 3,561
  • 2
  • 10
  • 26
0

Some terminals like your second example and the Python IDLE shell don't support colour output, namely ANSI escape codes. Therefore, simply use a terminal that supports colours like your first option.

As suggested elsewhere, Colorama is a great Python package, but you may want to consider other, simpler options. In full disclosure, I'm the author of the Colorist package. Just write:

from colorist import blue

blue("Hello World")

enter image description here

Or if you want a little more control over the output, you can also do this:

from colorist import Color

print(f"Only {Color.BLUE}this part{Color.OFF} is in colour")

enter image description here

Jakob Bagterp
  • 450
  • 4
  • 12