3

I have a python program where I am trying to print "Hello" in colored text using ANSI codes in command prompt. When I print in normally, it is not working, it just prints ? and text, but when I print it after clearing the command prompt it works fine. Can some one explain this weird nature.

I searched for this but couldn't find anything about this nature. I am using Windows 10

The below code outputs [0;32mHello[0m

print('\x1b[0;32m' + "Hello" + '\033[0m')

no color

whereas the below code outputs Hello in green color.

os.system("cls")
print('\x1b[0;32m' + "Hello" + '\033[0m')

Right image is the output of the code shown above or in the left image enter image description here  enter image description here

My expectation was that is gives either ansii or colored text in both the above codes, but actual output is different in both the cases

martineau
  • 119,623
  • 25
  • 170
  • 301
Sriteja Sugoor
  • 574
  • 3
  • 13
  • Which python version are you using? – Esdras Xavier Jun 27 '19 at 14:34
  • Could you please make a test with **sys**? This is the [code](https://gist.github.com/EsdrasXavier/699e263ed78333b4df6bdd7cab99da75) – Esdras Xavier Jun 27 '19 at 14:39
  • @EsdrasXavier, I tested the code you gave me, it gave same output as shown in the images above and my python version is 3.7.2 – Sriteja Sugoor Jun 27 '19 at 18:31
  • 1
    Well, I did some research about it, and looks like this could be a Windows issue. What you can do is use colorama, seems to solve the windows issue converting the asc code for each windows version. Take a look at this [link](https://www.devdungeon.com/content/colorize-terminal-output-python) – Esdras Xavier Jun 28 '19 at 10:40
  • Possible duplicate of [How to make win32 console recognize ANSI/VT100 escape sequences?](https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences) – snakecharmerb Jun 28 '19 at 18:42

2 Answers2

0

try this:

os.system("")
print('\x1b[0;32m' + "Hello" + '\033[0m')
Izaz
  • 85
  • 1
  • 2
  • 10
  • 2
    An explanation is required with the code, then only that will be called an answer. – letsintegreat Feb 19 '20 at 10:43
  • This *is* an answer, imo. But it tends to be an answer which is not helpful without more explanation. You could easily add some more context: E.g. why the difference `os.system("")` vs. `os.system("cls")` in OPs question? How and why exactly will this solve the problem? – colidyre Feb 19 '20 at 13:19
  • Sry, I wrote my comment in wrong section. I dint had enough 'reputaion' in stackoverflwo to (one needs atleast 50 reputaion) to write a comment. Thats why I wrote my comment in Answer section – Izaz Feb 19 '20 at 14:42
0

This must be an internal error in Windows 10. I tried to use Java to recreate your problem and came to the conclusion that this error can be fixed by enabling the virtual terminal processor, which interprets ANSI characters using the software encoder, or you can just use the new Windows Terminal (it uses PowerShell as the default CLI).

Hint: This is the registry entry that enables the virtual terminal processor:

[HKEY_CURRENT_USER\Console]
"VirtualTerminalLevel"=dword:00000001

If you don't want your users to address this issue by themselves, you can try these solutions:

  1. You can try to directly write the bytes of the ANSI sequence to the console output stream (equivalent to String.getBytes() in Java), which should work on the Windows 10 Command Prompt.
  2. You can run the cls command at the start of your program to get an unstable fix of the error.
  3. You can use the Windows > Console API > SetConsoleMode method to temporarily enable the virtual terminal processor (ENABLE_VIRTUAL_TERMINAL_INPUT: 0x0200).
MomoTheDev
  • 435
  • 1
  • 4
  • 8