Colorama for Python, Not returning colored print lines on Windows

前端 未结 7 1513
南笙
南笙 2020-12-01 18:16

I\'ve installed colorama for python. I\'ve imported the module as follows:

import colorama
from colorama import init
init()
from colorama import Fore, Back,          


        
相关标签:
7条回答
  • 2020-12-01 18:34

    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)
    
    0 讨论(0)
  • 2020-12-01 18:38

    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')
    
    0 讨论(0)
  • 2020-12-01 18:44

    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.

    0 讨论(0)
  • 2020-12-01 18:44

    Try The following:

    import colorama
    
    colorama.init()
    print colorama.Fore.GREEN + " Hey, im green! "
    
    0 讨论(0)
  • 2020-12-01 18:45

    I realise this is a very old question, but none of the existing answers helped me, so I'm posting my solution in case others are in the same boat. In my case, the problem was that I was importing stdout from sys and then initialising colorama, which does not work:

    >>> from colorama import Fore, Style, init
    >>> from sys import stdout
    >>> init()
    >>> stdout.write(Fore.RED + Style.BRIGHT + "Test" + Style.RESET_ALL + "\n")
    [31m[1mTest[0m
    

    Per https://pypi.org/project/colorama, this is because:

    On Windows, colorama works by replacing sys.stdout and sys.stderr with proxy objects, which override the .write() method to do their work.

    Thus, I need to import stdout after it has been replaced as part of the call to init:

    >>> from colorama import Fore, Style, init
    >>> init()
    >>> from sys import stdout
    >>> stdout.write(Fore.RED + Style.BRIGHT + "Test" + Style.RESET_ALL + "\n")
    Test      <--- This is now bright red.
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-01 18:52

    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.

    0 讨论(0)
提交回复
热议问题