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:
- 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.
- 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
- 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.
- 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.
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.
The user tcp2008 recommended to run:
import colorama
colorama.init()
print colorama.Fore.GREEN + " Hey, im green! "
Which returns [32m Hey, im green!
.