Printing colored caraters using PYTHON modules

Hi everyone,

I would like to add in my PYTHON script some colored caracters. For example, errors messages will be in red. I installed colorama and colorlist modules but when i print there is no colored caracters.

from colorist import red

red("Hello World")


from colorama import Fore, Back, Style
print(Fore.RED + 'some red text'

Here are the results : The sentences are in blue.

[34mHello Worlde[0m

e[31mHello Worlde[0m

Thank you in advance.

Neither your code, nor the python.exe process, is actually responsible for displaying the text - that’s done by the terminal. So the only thing your program can do is change what data it outputs, and the terminal can interpret that data as a command to change the colour - if it supports that. The purpose of packages like colorama is to make it easier to send those commands, but Python has no way to either know or control whether the commands are actually understood or obeyed. (This is the same issue for command-line programs in every programming language.)

To fix this, we need to understand what terminal you are using. It may be possible to change some settings, but you may need to use a different terminal completely.

If you need absolute control (i.e. the program has to make sure of what the output looks like, and cannot depend on the terminal), your only real option is to make a GUI program instead (you can even go so far as to make the GUI implement a terminal). This is, of course, much more complex.

Of possible interest: Textual framework

In the case of WIndows 10 and the CMD.exe terminal you have to switch into the mode that allows the escape sequences to be interpreted as colours.

My colour-text library as a init call to set this up.
On Windows it does the required Win32 calls.
On linux etc there is nothing to do.

See colour-text · PyPI

Oh, right, thanks for the reminder. Colorama has something for this. Since 0.4.6, colorama.just_fix_windows_console() should make those calls. There’s also a colorama.init() which… does more things. See the documentation on GitHub for details.

The question is, where you print your text. Skinning could be influenced by that environement.