Python colorama not working with input?

旧街凉风 提交于 2019-12-05 08:14:53

On my system, input() works with colors if you add

import sphinx.quickstart

to your module.

So here is the full code.

from colorama import Fore
import colorama
import sphinx.quickstart
colorama.init()
launch = input(Fore.GREEN + "Launch attack? (Y/N): ")

(This leads to two questions:

  1. Why does it not work in the first place?
  2. What is the actual reason? – Someone might like to dive into the sphinx source code.)

N.B. if you run python via winpty from Git Bash, set convert.

colorama.init(convert=True)

Otherwise, you do not get color with the current versions.

I had this same issue (Python 3.5.4) and, just in case it is not too obvious for somebody else looking at this, you can always rely on the workaround of combining print / input calls where you previously had just an input call:

print(Fore.GREEN + "Launch attack?(Y/N): ", end='')
launch = input()

This should produce the exact same output as in your question, with no extra blank lines and with the code coloring working without the need of importing anything else.

The (small?) disadvantage is that you you will end up with two lines of code where you previously had just one.

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