44

I'm looking for a way to change the color of the text output from my python scripts as it runs. The basic idea is something like this:

if (Data < LowerLimit):
    print "Failed" # Output Failed as Red Text
elif (Data > UpperLimit):
    print "Failed" # Red Color
else:
    print "Passed" # Blue Color

The scripts are being used on windows machines for quick data analysis.

martineau
  • 112,593
  • 23
  • 157
  • 280
CyanRook
  • 7,774
  • 4
  • 19
  • 18

4 Answers4

72

Or about the best module I have found http://pypi.python.org/pypi/colorama

Jakob Bowyer
  • 32,237
  • 8
  • 73
  • 89
  • 2
    Yes - colorama actually works for the Windows 7 command prompt. Prior to windows 10, the ANSI codes don't work (for some inscrutable reason) in the command prompt. Colorama can use Win32 commands entirely transparantly - which makes it work and makes it easy to use. I'm glad I finally found a solution to this problem. – Noise in the street Apr 26 '17 at 11:25
  • @Noiseinthestreet Can you this this => https://stackoverflow.com/a/70599663/3057246 – Vinod Srivastav Jan 18 '22 at 08:39
17

This is extremely simple! Rather than importing odd modules for python or trying long commands you can take advantage of windows OS commands.

In windows, commands exist to change the command prompt text color. You can use this in python by starting with a: import os

Next you need to have a line changing the text color, place it were you want in your code. os.system('color 4')

You can figure out the other colors by starting cmd.exe and typing color help.

The good part? Thats all their is to it, to simple lines of code. -Day

Daymarquez
  • 325
  • 2
  • 3
  • 26
    That changes the colour of the whole console, not just the text printed afterwards. – zvone Mar 03 '13 at 03:57
  • Sorry about that guys. I was sleepy and totally misunderstood the question. – Daymarquez Jul 10 '15 at 21:14
  • It's still a valid answer to the EXTREMELY RELATED question LIKELY TO GET MARKED DUPLICATE which is how to change shell text color in Python... – Ian Oct 26 '16 at 17:30
  • Upvoted, because while it might not exactly answer the original question, this does answer my question for which Google brought me here. – szmate1618 Dec 08 '18 at 12:41
  • 15
    Actually, if you run os.system('color'), then the ANSI escape sequences magically start working in windows. – Szabolcs Dec 12 '18 at 16:48
2

Try to look at the following link: Python | change text color in shell

Or read here: http://bytes.com/topic/python/answers/21877-coloring-print-lines

In general solution is to use ANSI codes while printing your string.

There is a solution that performs exactly what you need.

Community
  • 1
  • 1
Artsiom Rudzenka
  • 26,491
  • 4
  • 32
  • 51
  • But why downvote? What is wrong with my answer???? – Artsiom Rudzenka Jun 30 '11 at 16:02
  • 2
    I have tried their solutions, however they do not appear to work with windows. The top answer (the hilite function) when called hilite("Hello", True, False) results in '\x1b[32mhello\x1b[om' which when printed results in ←[32mhello←[0m instead of printing in color. Also the curses library is not on windows platforms. – CyanRook Jun 30 '11 at 16:03
  • Sorry if so, i have checked it in windows with pycharm and it works perfectly, however it doesn't work with python console, so please forgive and bear with me – Artsiom Rudzenka Jun 30 '11 at 16:14
  • 1
    Although workable, printing raw ANSI codes is a 'crufty' solution at best. You should not expect it to work well in all consoles. – jforberg Jun 30 '11 at 16:18
-3

Been looking into this for a while and not got any satisfactory answers, however...

1) ANSI escape sequences do work in a terminal on Linux

2) if you can tolerate a limited set of colo(u)rs try this:

print("hello", end=''); print("error", end='', file=sys.stderr); print("goodbye")

In idle "hello" and "goodbye" are in blue and "error" is in red.

Not fantastic, but good enough for now, and easy!

Neil
  • 11
  • Also just tried it on Windows - works the same there in an idle "terminal" also forgot you need to: 'import sys' – Neil Sep 15 '13 at 20:31
  • 3
    That's just IDLE coloring stdout and stderr separately. This won't do anything in a normal console (where scripts are usually run) – Jonathon Reinhart Nov 02 '13 at 04:59