1

I am trying simply to use cprint in a very simple command :

import sys
from termcolor import colored, cprint
cprint("xxxxxxxx", 'blue', 'on_yellow')

               [43m[34mxxxxxxxx[0m
cprint("xxxxxxxx", 'blue')
               [34mxxxxxxxx[0m

however, I get no blue color and I get the strings "[34m' and '[0m' (before and after the string 'xxxxxx'. I have imported (as seen) the sys and the termcolor. I am using pip version 19.2.3. Please help! thanks, Mike

Mic
  • 21
  • 3
  • This might be because you're trying this in a command shell that doesn't support this. Try running this as a script from the bash shell (or cmd if on windows). Or do it interactively from a python session started from the command shell (bash/cmd). – Todd Apr 11 '20 at 23:18
  • Thanks Todd, but I am trying with the cmd shell in windows 10. . any further ideas will be welcomed. – Mic Apr 12 '20 at 06:09
  • Oh.. Windows. Pulling up the pypi page for termcolor, I notice that the Windows row in their table of supported modes seems to indicate that the support is limited. https://pypi.org/project/termcolor/ If i'm interpreting this correctly... – Todd Apr 12 '20 at 06:13
  • I found this answer on the topic of ANSI support for its shell https://stackoverflow.com/a/16799175/7915759 if you can't get it working for the native cmd shell, there are alternatives, like ansicon. Check this out too: https://en.wikipedia.org/wiki/ANSI_escape_code#DOS_and_Windows look at the last paragraph (dos and windows section). It has relevant info on this. – Todd Apr 12 '20 at 06:18
  • Hi Todd, Seems too complex for me and i'm afraid to "touch" any configuration in the windows on my PC :(. the issue is that which ever method or library I use, then instead of getting the output string in color, I get the string in default color but overlapped with the \033 control characters. like for example: if I write : text = colored('Hello, World!', 'red', attrs=['reverse', 'blink']), then print(text), then I get : [5m[7m[31mHello, World![0m … really frustrating . – Mic Apr 12 '20 at 14:31
  • Are you constrained to use the native command shells, or do you have the freedom to use a 3rd party solution? – Todd Apr 12 '20 at 14:51
  • yes, sir. its a part of a python course which my performance are tested on standard CMD and python installed on windows 10. the funny thing is that it worked once with the same commands and suddenly something happened which I can see a symptom of default color overlapped with various \033[1;37m like permutations . it seems something related to win10 but I don't have any clue what.. – Mic Apr 12 '20 at 15:59
  • hmm.. looks like some posters in comments have been saying there's a simple registry hack, but if you're afraid to try it, then I"m out of ideas. I don't have my win10 system up so I can't experiment. Hopefully, someone will know and answer here. Or you could try posting on the MS site. – Todd Apr 13 '20 at 00:00
  • Hi Todd, I have managed to solve the problem using the following function : def init_ansi_colors(): try: import colorama colorama.init() except: try: import tendo.ansiterm except: pass which turns on the ANSI colors in windows environment. thanks for your great help so fat ! – Mic Apr 14 '20 at 08:42
  • Awesome - glad you got it working! – Todd Apr 14 '20 at 08:45

2 Answers2

0

Warning: This answer assumes you are using Windows 10 or newer

If you are indeed using windows, you can make termcolor work by using this code at the beginning of your program:

import os
os.system("color")

This initializes the Windows Command Prompt to print colours instead of the ANSI codes.

Enderbyte09
  • 409
  • 1
  • 5
  • 11
0

Instead of using cprint, use the original python print and used 'colored' instead, like this:

print(colored("xxxxxxxx", "blue"))

Please ask me about any problems or clarifications.