2

I'm trying to change the output text to green and the background to blue using this code:

print("\033[1;32;44m******************************************************\n")
print("\033[1;32;44m* Terminate Instance Operations in AWS Are Complete. *\n")
print("\033[1;32;44m******************************************************\n")

But the output is the the default powershell white and the numeric codes are visible. This is how the output looks:

[1;32;44m******************************************************

[1;32;44m* Terminate Instance Operations in AWS Are Complete. *

[1;32;44m******************************************************

I'm running the script on win 10 currently. What am I doing wrong?

2
  • What version of Windows are you on? Windows 7 conhost does not support VTY escapes. Additionally, earlier versions of the Windows 10 conhost did not support VTY either. Commented Mar 4, 2019 at 16:56
  • I'm on Windows 10 version 1709. I can also try running it under a linux VM to see if it looks any different. Commented Mar 4, 2019 at 16:59

2 Answers 2

4

Depending on your version of Windows 10, your console host may not support VTY emulation which enables you to use those escape sequences. You can work around this as such:

import os

os.system('COLOR 12')

See cmd.exe /c COLOR /? for more.


To see if your console has the support, enter this command in PowerShell:

$Host.UI.SupportsVirtualTerminal
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! That works great! Yes giving that last command tells me I do have color support on the console. Appreciate the info!
@bluethundr, virtual terminal support is not enabled by default. CMD and PowerShell enable it for themselves but revert to the default when running an external program such as python.exe. We can enable it by default by setting "VirtualTerminalLevel" in "HKCU\Console" to 1 as a DWORD value. Or in Python we can use ctypes or PyWin32 to call GetConsoleMode and SetConsoleMode to enable it manually.
@ErykSun interesting. Could you provide an answer with an example?
@not2qubit sp HKCU:/Console VirtualTerminalLevel 1 -Type DWord
It can be any non-zero value according to this documentation
0

you can use termcolor: pip install termcolor

then:

from termcolor import colored

print(colored(<text>, <color>))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.