3

The standard command to color the output of print in Python 3 using ANSI escape codes seems not to be functioning.

It has to do with the changes that accompany the newest versions of Python 3, I believe.

My System: Python 3.7.3, Windows 10, IDLE.

Although it works fine on Python 2.7, with Python 3.7 it simply doesn't function.

  • print("\033[1;32;40m Bright Green \n") in IDLE as shown in this article outputs [1;32;40m Bright Green. On the contrary this works fine on an online Python 2 parser giving a colored background.

  • A correction was found that on Python 3 the escape character is \x1b instead of \033. A modified expression inside the print statement such as print("\x1b[1;32;40m Bright Green \n") in IDLE outputs [1;32;40m Bright Green and no colored background.

  • Running print("\033[1;32;40m Bright Green \n") on a py file still outputs [31;1;4mHello[0m
  • Since there are differences between my local system I tried to run on an online Python 3 parser print("\033[1;32;40m Bright Green \n") and it gave me [1;32;40m Bright Green.

This use of ANSI escape codes is mostly offered as a solution in links.

What do I do wrong?

ExoticBirdsMerchant
  • 1,466
  • 8
  • 28
  • 53
  • *"has to do with the changes that accompany the newest versions of Python 3"* - which version are you on, and which versions did it work in? What platform? – jonrsharpe Jun 05 '20 at 10:35
  • My version is Python 3.7.3 and the platform is Windows 10 – ExoticBirdsMerchant Jun 05 '20 at 10:36
  • 2
    And did this work in earlier versions that that? Have you read e.g. https://stackoverflow.com/q/12492810/3001761? – jonrsharpe Jun 05 '20 at 10:36
  • No i will read this one now – ExoticBirdsMerchant Jun 05 '20 at 10:37
  • Dear @jonrsharpe is still isnt working i imported colorama and did the code in the accepted solution printed `File is: [1;31m[0;0m`. The background was not colored. I think its a but in Python 3.7 – ExoticBirdsMerchant Jun 05 '20 at 10:41
  • @jonrsharpe It works fine on Python 2.7 i have tried on an online parser – ExoticBirdsMerchant Jun 05 '20 at 10:43
  • 1
    There are likely *other* differences between your local environment and an online parser - does the online parser show the colours in Python 3? Does Python 2 IDLE show them locally? What about outside of IDLE? – jonrsharpe Jun 05 '20 at 10:44
  • @jonrsharpe i am trying to do what you say and i am integrating your comments on the question. One addition that on a `py` file it does not work is in the question – ExoticBirdsMerchant Jun 05 '20 at 10:53
  • What does *"on a `py` file"* actually mean? *"it works fine on Python 2.7"* - is that still from the online parser, or locally? It's not clear whether you're making fair comparisons. – jonrsharpe Jun 05 '20 at 10:55
  • @jonrsharpe i have a file under site-packages named `test.py` An empty code module can be opened with the `New File` option from the `IDLE GUI`. That is `File > New File` – ExoticBirdsMerchant Jun 05 '20 at 10:57
  • 2
    I can confirm in IDLE 2.7 that `print("\033[1;32;40m Bright Green \n")` prints into the console `[1;32;40m Bright Green ` (the tkinter `Text` widget cannot handle the escape and the functionality hasn't been manually added to IDLE). – Minion Jim Jun 11 '20 at 09:33
  • So where is the problem to blame? The IDLE? Is the command sound but the IDE isnt working? @MinionJim – ExoticBirdsMerchant Jun 11 '20 at 12:30
  • I cannot find a problem, other than Windows itself, but this doesn't explain the difference between versions. I have tested `python -c "print('\033[1;32;40m Bright Green \n')"` on Windows (cmd and powershell) and Linux, but only the latter works as expected. I would recommend using `colorama`. – Minion Jim Jun 11 '20 at 12:42
  • Are you maybe hitting [this bug](https://bugs.python.org/issue29059)? It seems more people have complained about it on python 3.7. It may also be IDLE's terminal doesn't support ANSI color codes, or dropped support for them – Leonardo Dagnino Jun 13 '20 at 17:13

2 Answers2

3

You need to use different code depending on if you are using terminal or IDLE. There are many ways to do it for the terminal, but for IDLE, below is the way I found.

Use sys.stdout.shell.write(to_print, color), with the variable colors being one of the strings below

You can use the below colors:

  • BUILTIN
  • STRING
  • console
  • COMMENT
  • stdout
  • stderr
  • hit
  • DEFINITION
  • KEYWORD
  • ERROR
import sys

try:
    color = sys.stdout.shell
    color.write("Hi? \n","STRING")
except AttributeError:
    raise RuntimeError("Use IDLE")
KetZoomer
  • 2,701
  • 3
  • 15
  • 43
3

The python colored library works without any issues. ANSI Escape codes are not as smooth.

After pip install colored you can then after from colored import stylized

then try- print(stylize("This is green.", colored.fg("green")))