3

ANSI code wont work on my python interpreter

I wanted to color some of my prints on the project. I looked up on how to color printed characters and found the ANSI escape codes, so i tried it up on the interpreter, but it wont work.

for example:

print("\033[32m Hello")

it Gives me <-[32m Hello (an arrow left sign).

how do i make it work? can it work on python's interpreter? if not, where should i use it?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Jhon Margalit
  • 451
  • 4
  • 12
  • 4
    I'm unable to reproduce this. When I put `print("\033[32m Hello")` in a file `test.py` and run `python2 test.py` or `python3 test.py` in my Unix terminal, I get `Hello` written in green. I see that `\033` is left-arrow in CP437 though. Are you trying to do this on Windows? Have you already manually installed Windows software required to interpret ANSI escapes? – that other guy Aug 30 '19 at 22:40
  • 1
    may be your terminal application does not support it? What terminal app are you using? – Sam Daniel Aug 30 '19 at 22:43
  • 3
    The Python interpreter has nothing to do with ANSI escape sequences. Sounds like you're using Windows, so you'll need to install something that makes the WIndows console support them. – martineau Aug 30 '19 at 22:45
  • The OS has nothing to do with it, just the terminal emulator. – Barmar Aug 30 '19 at 22:48
  • @Barmar: It matters because it determines if the OS shell supports the ANSI codes. – martineau Aug 30 '19 at 22:52
  • 2
    @martineau The shell is not in I/O path between applications and the terminal. – Barmar Aug 30 '19 at 22:54
  • @Barmar: If the OP is on WIndows, then the shell is `cmd.exe` and what it provides the only "terminal" capabilities available (by default). That's why I suggesting finding and installing third-party software that will make it emulate an ANSI terminal. – martineau Aug 30 '19 at 22:58
  • You can google [windows ansi console emulator](https://www.google.com/search?hl=en&as_q=windows+ansi+console+emulator&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&as_filetype=&as_rights=) and find one. – martineau Aug 30 '19 at 23:04
  • 1
    @martineau Actually the builtin terminal (or "console" in Windows terms) is not provided by `cmd.exe` but by a combination of the console host (`conhost.exe`) and the ClientServer Runtime System Service (`csrss.exe`). See https://www.howtogeek.com/howto/4996/what-is-conhost.exe-and-why-is-it-running/ – Michael Butscher Aug 30 '19 at 23:13
  • @martineau I believe Python versions on or after 3.6 will bypass the shell entirely for console output so that they can support full Unicode. This means that ANSI sequences will never work no matter how many patches you install. – Mark Ransom Aug 31 '19 at 02:41
  • @MarkRansom, would you have a source for that, please? Thanks. – nyov Aug 31 '19 at 06:10
  • @nyov: https://www.python.org/dev/peps/pep-0528/ – Mark Ransom Aug 31 '19 at 13:54
  • @MarkRansom, ah thanks. Windows API. Since you said "bypass the shell" I was wondering how that could even work... But I think ANSI escapes will continue to work even on windows, [somehow](https://superuser.com/a/1300251/912095). – nyov Aug 31 '19 at 14:17
  • 1
    @nyov that's an interesting link. But without seeing it work for myself I'd hesitate to to predict that it would. The path from console I/O to screen is simply too convoluted. – Mark Ransom Aug 31 '19 at 14:27
  • Those are a lot of topics that im not familiar with. I mean, I started studying python just two months ago and I have somehow got into this topic (coloring), so all of those names "Windows API" and "terminal" vs "consol"... they tell me nothing for now, so I would much appriciate it if someone would explain what are you all talking about. – Jhon Margalit Sep 06 '19 at 21:35

3 Answers3

1

Note, this is a possible duplicate to this question answered by this post. A reiteration of the answer by @gary-vernon-grubb is posted below for convenience.


Use os.system('') to ensure that the ANSI escape sequence is processed correctly. An example in the Windows Command Prompt can be seen below:

An example of ANSI codes being improperly processed and then properly processed after running the command os.system('').

Ensure that there are no spaces between the ANSI escape sequence and the color code! This was a bit of a pain in the neck for me.

roomrys
  • 81
  • 1
  • 6
0

You are best off installing other packages to help generate the ANSI sequences, iirc win32 console does not support ANSI colours natively. One option is to install colorama. The following snippet prints out red text.

import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Fore.RED + 'This is red')

Edit: Upon researching a little more, I've realised that Windows 10 has support for ANSI Escape Sequence and you can do so by calling. Use this if you intend on copy and pasting.

import os
os.system("echo [31mThis is red[0m")

However i'd still prefer the former.

Axois
  • 1,961
  • 2
  • 11
  • 22
0

You could be using IDLE... in that case you can't have ANSI colours; the IDLE 'terminal' isn't really a terminal so ANSI codes will show up as a character, whether you type chr(0x1B) or \033 or \x1b; It's all the same.

Your arrow character is normal; I just get a box because I guess the default font doesn't support left arrows...?

But @thatotherguy's explanation might be right... unless you're using IDLE because in that case it's definitely the problem.

Arale
  • 39
  • 1
  • 8