[Tutor] Problem using termcolor and colorama

Peter Otten __peter__ at web.de
Thu Nov 15 15:58:40 EST 2018


John Blower wrote:

> I try to create colour text by using these two  modules , only returns the
> colour code, no colour text. How to solve this problem.
> 
>>>> import termcolor
>>>> import colorama
>>>> colorama.init()
>>>> termcolor.cprint('Hello World','red')
> [31mHello World[0m

Start with one module. Does

>>> import colorama
>>> print(colorama.Fore.RED + "Hello" + colorama.Style.RESET_ALL)
Hello

print Hello in red?

If yes, how do

>>> colorama.Fore.RED + "Hello" + colorama.Style.RESET_ALL
'\x1b[31mHello\x1b[0m'

and

>>> termcolor.colored("Hello", "red")
'\x1b[31mHello\x1b[0m'

differ on your system?

If no, does

>>> from ctypes import windll
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'windll'

raise an exception on your system? This is expected on my (Linux) system, 
but on Windows it probably means that something went wrong.
The pypi page says

"""
I’ve personally only tested it on Windows XP (CMD, Console2), Ubuntu (gnome-
terminal, xterm), and OS X.
"""



More information about the Tutor mailing list