1

I'm trying to print coloured text with Colorama, and I have tried this:

from colorama import Fore, Back, Style

print(Fore.RED + 'some 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')

But it returns this:

[31msome red text
[42mand with a green background
[2mand in dim text
[0m
back to normal now

When I want:

  • The first line in red
  • The second with a green background
  • The third in dim text
  • And then all back to normal.

I'm using python 3.7.3, with the latest colorama download from pip, on a Windows 10 computer.

.

EDIT: My question was marked as a duplicate, with 6 already posted answers, so here is what happens if I use them:

  1. Answer from hrbdg instructed to run this code:
from colorama import init, Fore, Back, Style

init(convert=True)

print(Fore.RED + 'some red text')

For me, it returns some red text in the standard blue colour.

  1. According to Sean Lynch I need to patch it. So I download Ansicon and follow his/her instructions. Then I print:
from colorama import Fore, Back, Style

print(Fore.RED + 'some 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')

Again I get:

[31msome red text
[42mand with a green background
[2mand in dim text
[0m
back to normal now
  1. Answer from babrar instructed to run this code:
from colorama import init
from termcolor import colored

init()

print(colored('Hello, World!', 'green', 'on_red'))

For me, it returns [41m[32mHello, World![0m in blue colour.

  1. The user sorin suggests using tendo.colorer because I don't have ANSI on Windows. So I download tendo via pip. According to him/her: "just importing it before your code it will fix the problem". So I run this code:
from colorama import Fore, Back, Style

from tendo import colorer

print(Fore.RED + 'some 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')

But that returns... suprise [31mMy Text is Red, and hopefully I don't have a virus in my computer now.

  1. The user kamzur says that Mike, the original poster of the question, only needs to use one import in one line instead of in three lines. This is not helpful.

  2. The user tcp2008 recommended to run:

import colorama

colorama.init()
print colorama.Fore.GREEN + " Hey, im green! "

Which returns [32m Hey, im green!.

Dux
  • 11
  • 3
  • 1
    Impressed by a new user to write such a detailed non-duplicate explanation instead of the classic *"I tried other answer but didnt work"*. Keep up the good work @Dux! – Nino Filiu Jun 06 '19 at 18:29
  • 4
    The most important part is still missing: how do you **run** the code? The thing is, it *should* print something like `[31m`, but the terminal emulator you run the code in is expected to understand this as an ANSI escape sequence. Yours apparently doesn't, so maybe the issue lies in this. I see you mention Windows, is it `cmd.exe` or some other emulator? – Norrius Jun 06 '19 at 20:16
  • 1
    Red text is selected as "[31m", so if you are seeing "[31m" it is outputting correctly but Windows is not interpreting it correctly. You might have a look at https://stackoverflow.com/questions/44047988/windows-10-console-colors-not-working-virtual-terminal-control-character-sequen – Alcamtar Jun 06 '19 at 20:54
  • Because you mentioned standard blue colour, I assume you are using IDLE? If so colorama does not work in IDLE, it is only available in the interpreter, (see the [comment thread](https://stackoverflow.com/a/44775020/7147233) on my answer) – WhatsThePoint Jul 08 '19 at 14:59
  • I'm voting to close this. While it's reproducible, it is the expected output depending on what IDE is used. It just depends on whether the IDE correctly interprets the escape character. For example, this prints colored or highlighted text correctly in a Jupyter Lab cell. – Trenton McKinney Jun 14 '21 at 21:08

0 Answers0