output of [31m text instead of color
Asked Answered
D

2

14

I am trying to print coloured text with colorama but when I compile an exe and run following...

from colorama import Fore, Back, Style
print(Fore.RED + 'text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')
I get output of::

Output:

[31mtext
[0m
back to normal now

Is it possible to print colors when compiling to pyinstaller exe or is this simply not possible?

Dashboard answered 22/11, 2017 at 10:33 Comment(2)
Something is dropping or ignoring the escape character, but you need to tell us more about how you are running this to allow us to figure out why that's happening. For a start, which version of colorama, installed how, on which platform?Plash
@Plash I'm using windows and most recent version of colorama. I simply use: Pyinstaller -F color.pyDashboard
P
18

On Windows, you have to initialize Colorama with colorama.init() (see the second line):

from colorama import init, Fore, Back, Style
init()
print(Fore.RED + 'text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

I have tested this code in cmd and PowerShell and it produces the expected colored output.

From Colorama docs:

On Windows, calling init() will filter ANSI escape sequences out of any text sent to stdout or stderr, and replace them with equivalent Win32 calls.

On other platforms, calling init() has no effect (unless you request other optional functionality; see “Init Keyword Args”, below). By design, this permits applications to call init() unconditionally on all platforms, after which ANSI output should just work.

Prawn answered 19/12, 2017 at 15:16 Comment(0)
S
3

cmd.exe of Windows doesn't support ANSI escape sequences.

This topic on superuser might helps if you want these be intepreted by cmd.exe natively http://superuser.com/questions/413073/windows-console-with-ansi-colors-handling/

So pure crayons might not works under cmd.exe of Windows.

However according to the documentation of colorama

This has the upshot of providing a simple cross-platform API for printing colored terminal text from Python, and has the happy side-effect that existing applications or libraries which use ANSI sequences to produce colored output on Linux or Macs can now also work on Windows, simply by calling colorama.init().

Try using ConEmu. You might be able to do it

Saltworks answered 14/12, 2017 at 6:5 Comment(4)
Can this print colored text that would also work if I ran it on my laptop? Could you provide an example? Sounds interesting.Dashboard
I haven't tested it on windows. If not conemu, there are other packagesSaltworks
I see. I assume this would make the colors work on my pc, but if I used this on another external dependency would need to be installed which is not really ideal.Dashboard
If you are using it for production level software it's little not appropriate, as you need to add it as external dependency. For stand alone, it seems to be a good ideaSaltworks

© 2022 - 2024 — McMap. All rights reserved.