Colorama for Python, Not returning colored print lines on Windows

a 夏天 提交于 2019-11-28 10:56:33

I had this same issue on Windows 7 x64, I finally got the colors working without having to install anything new just by adding the argument convert=True to the init call.

from colorama import init, Fore, Back, Style

init(convert=True)

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

I've never had success getting colors working in Windows cmd.exe without patching it with Ansicon. After patching, ANSI color codes will work without needing to use something like colorama (which didn't work for me either).

To patch cmd.exe with Ansicon, do the following:

  1. Download Ansicon from https://github.com/adoxa/ansicon/downloads and unzip it into a directory with no spaces
  2. Use a cmd prompt and navigate to where you unzipped it.
  3. CD into the x64 directory (unless you have a 32bit machine, then use the x86 one)
  4. Type ansicon.exe –i
  5. Open a new cmd prompt

via: https://stackoverflow.com/a/4749307/191902

Also, if you have an NVidia graphics card, you might need to set the environment variable "ANSICON_EXC" to "nvd3d9wrap.dll".

I know I'm late, but this will hopefully help anyone still looking for the answer.

Stating from Colorama's documentation on PyPI:

Colorama can be used happily in conjunction with existing ANSI libraries such as Termcolor

from colorama import init
from termcolor import colored

# use Colorama to make Termcolor work on Windows too
init()

# then use Termcolor for all colored text output
print(colored('Hello, World!', 'green', 'on_red'))

This worked for me, on Anaconda Prompt (essentially cmd.exe) on Windows 10 64-bit.

Colorama's native ANSI sequences don't seem to work for some reason. An external ANSI library (i.e. Termcolor) did the trick for me.

That's normal because you do not have ANSI on Windows.

Try somehting like tendo.colorer and this will enable coloring for all platforms.

Note: tendo.colorer adds coloring to the logs, but I'm sure you will figure it out on how to use it for other things. If I'm not mistaking just importing it before your code it will fix the problem.

you can use the import only one import. such as:

from colorama import init, Fore, Back, Style

init()

and you can try it now :

print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Fore.RESET + Back.RESET + Style.RESET_ALL)

Try The following:

import colorama

colorama.init()
print colorama.Fore.GREEN + " Hey, im green! "
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!