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
- Fix issue #103 - work around Python bug in input function — committed to tartley/colorama by deleted user 8 years ago
- Revert "Fix issue #103 - work around Python bug in input function" This reverts commit 17eef5991b393ad3a440071d57a9cfec703c5902. — committed to tartley/colorama by deleted user 7 years ago
This workaround is a little clumsy but prints “test” in red. Works on Windows 10 and Linux.
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:
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
init()ordeinit()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: