colorama: colorama not working with input(...)

System: Windows 7 Shell: cmd.exe Python: 3.5.1 Colorama: Current master (69e4069)

For example:

from colorama import Fore, init
init()
input(Fore.RED + "test")

This prints ←[31mtest instead of of “test” in red.

There is a similar question on Stack Overflow without any good answer: http://stackoverflow.com/q/32872612/11722

And I can verify that this worked in Python 2.7.

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Comments: 18 (3 by maintainers)

Commits related to this issue

Most upvoted comments

This workaround is a little clumsy but prints “test” in red. Works on Windows 10 and Linux.

from colorama import Fore, init
init()
print(Fore.Red + "")
input("test")

To get rid of this problem in the starting of the code add

**import os

os.system(‘cls’)**

This will clear the screen and hence clear all the external factors blocking the colorama in input. This works 100% you just need to do it once in the starting of the program [or just before the first use of colorama with input] and after that use colorama in any creative way you want to.

I hope this will help all the creative minds trying to make their codes more colourful

The easiest/cleanest thing to do as a workaround is to replace any input(message) calls with a custom function that simply prints the message (which we know works) and then pauses via a regular input() like so:

def input_colorama(message) :
    print(message, end = '')
    input()

The function above has the exact same output and behavior as regular input(message) would.

Hi @al3xv3gas . I pushed the workaround with the ‘fileno’ method, and submitted a new bug in the Python issue tracker: http://bugs.python.org/issue28373 Please try the latest version in this repository and let me know if it works now.

This issue is only with the default Command Line provided with windows. Tested OS: Windows 10 Home, Build 17134 Tested Python Version: 3.7.0

Using input(Fore.CYAN + Style.BRIGHT + "Input State: ") on Command Line

image If you really need to use that one, use the workaround posted by @juliedwhite

Using input(Fore.CYAN + Style.BRIGHT + "Input State: ") on Cmder for Windows

image Another benefit to using Cmder is that it does not require you to use init() or deinit() due to native colour support.

I’d suggest using the terminal instead of IDLE or another editor. IDLE (3.5.2) didn’t work for me - macOS terminal did.

Looks like it’s broken in Python 3.5, probably related to this change: http://bugs.python.org/issue24402 The input function now doesn’t print to the wrapped stdout because it’s different than the original. It’s a little ironic because it sounds like the Python issue was supposed to work exactly for our case, and now it doesn’t unless we add this method to StreamWrapper:

    def fileno():
        raise OSError()