2

I know this question have been raised at Stackoverflow here

So I've tried the following code from joeld's answer.

Here is the code in IDLE (I am using Python 2.7):

print '\033[95m'+'my text'+'\033[95m'

and I got the output without color changed:

[95mmy text[95m

===============================================================================

Then I also tried the package colorama. The package was installed within cmd:

python setup.py install

and tried following code in Aptana Studio 3:

from colorama import *

print (Fore.GREEN + 'Green text')
print (Fore.Red + 'Red text')

I got following output:

[32mGreen text
Traceback (most recent call last):
  File "C:\Users\My Documents\Aptana Studio 3 Workspace\Practice\test_colorama.py", line 12, in <module>
    print (Fore.Red + 'Red text')
AttributeError: 'AnsiCodes' object has no attribute 'Red'

===============================================================================

So now I am really confused. These solutions are upvoted hundreds of times which implies that it should be effective, but in my case it seems not.

May I know how I can print colorful text in the terminal or console?

Many thanks.

Community
  • 1
  • 1
ChangeMyName
  • 7,018
  • 14
  • 56
  • 93

2 Answers2

3

There's no Red

>>> import colorama
>>> colorama.Fore.Red
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'AnsiCodes' object has no attribute 'Red'

But, RED exists:

>>> colorama.Fore.RED
'\x1b[31m'
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Hi, Flasetru. Thanks for the answer. I've tried to change `Red` into `RED` and there is no more error report. However, the color of text in the Console of Aptana Studio 3 remains black. – ChangeMyName Mar 04 '14 at 10:18
  • @ChangeMyName, `colorama` packages is for normal terminals/terminal emulators. Try the code in terminal (not in IDLE, Apatana, ...) – falsetru Mar 04 '14 at 10:26
  • I've tried `colorama` in Apatana Studio 3. The output is `[32mGreen text` and no color change. – ChangeMyName Mar 04 '14 at 10:29
  • @ChangeMyName, I'll not repeat the same comment. :( – falsetru Mar 04 '14 at 10:33
1

IDLE is not a proper shell. Do this from a Python session in a normal terminal.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895