I am trying to write for both NT and POSIX (my two usual platforms) and have come up with the code below. The clrs code I refer to is apparently from blender build scripts, I discovered it on this SO post. They called the class bcolors but I wanted to reduce clutter so I renamed it.

But the main class is what I've written that determines the os before printing. Prints are called, for example, via PRN.cyan("some string").

For this to work I import colorama:

from colorama import init, Fore, Back, Style

The PRN class that handles the print calls:

class PRN:
    def cyan(p_str):
        """print some text with cyan foreground"""
        if os.name == 'nt':
            # we'll try the Colorama option but if it fails we can
            # change the global variable for colours.
            if coloursOn:
                sys.stdout.write(Fore.CYAN + p_str + Fore.RESET)
            # try Deez1.clrs for posix environment
            sys.stdout.write(clrs.cyan + p_str + clrs.ENDC)

If colorama doesn't work on a Windows system for some reason I can change the global boolean coloursOn.

Is this an incredibly klutzy/dumb implementation? From what I've read, so far, cross platform capability for ANSI color printing has only gotten shakier, not simpler.

  • 1
    \$\begingroup\$ Much as I hate to say it as an Irishman, it's probably better you use colorsOn for the sake of consistency. Using both spellings is liable to create a typo. \$\endgroup\$ Aug 20, 2015 at 11:58
  • \$\begingroup\$ @SuperBiasedMan true. \$\endgroup\$
    – Dee
    Aug 20, 2015 at 22:15
  • \$\begingroup\$ @Jamal Thanks for the edits; so because I've tagged the question with python there's no need for it in the title? Apologies for the lack of initial capitals - I'm anti-capitalism but I'll try to refrain from that when discussing code. \$\endgroup\$
    – Dee
    Aug 20, 2015 at 22:19

1 Answer 1


First off, "cross platform capability for ANSI color printing" doesn't quite make sense (if I understand correctly). ANSI color printing is only going to work on terminals that are ANSI compatible.

Therefore, it doesn't really matter what OS it is: if it's an ANSI terminal, you can use ANSI colored output.

Therefore, you can remove the checking of the operating system.

Note: according to this answer,

"All modern terminal emulators use ANSI escape codes to show colours and other things."

Judging by the name of the method your presented and the context of the code, you have created a separate method for each color.

  1. This sounds like you are repeating yourself a lot.

  2. It's not very flexible for the code that is using your library.

I recommend that you create a single method print_color_string that takes a string and the color to print.

def print_color_string(str, color):
    ansi_color = getattr(Fore, color)
    if ansi_color:
        sys.stdout.write(ansi_color + p_str + Fore.RESET)
        # throw error because color is not a valid color
  • \$\begingroup\$ thank you, valid points which I appreciate. The reason I came to this impasse was that when I executed my Python code, written on my Mac, on a Win7 machine the escape sequences which I believed were for ANSI printing, came out as just that, escape character sequences. Would I call that "un-parsed"? I then discovered the Colorama module which works for me on Win7/8 but IIRC not Win XP. Your improved code I will implement but rather than throw an error I think more suitable (for me in this application) to simply print the text without coloring it. \$\endgroup\$
    – Dee
    Aug 24, 2015 at 3:39
  • \$\begingroup\$ I followed the link to that SO answer which led me to the wiki page for ANSI which states "The Win32 console does not support ANSI escape sequences at all." so there do appear to be some incongruities in trying to use ANSI for my purposes at least. I realise my grasp of terminology when it comes to DOS 'command' windows, terminal emulators and Command Line Interfaces is lacking - the overlaps and their platforms etc. I'm aware of the differences but it's not ingrained in my awareness. \$\endgroup\$
    – Dee
    Aug 24, 2015 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.