6

When running python scripts or programs built using python, errors are emitted in dark red. As I get older, this is getting harder for me to read, to the point I have to squint, magnify, or fuss with the console properties and re-run commands.

I really dont want to change the console defaults because other programs generally dont have this problem, and it just seems to be Python that doesn't honor the hosting console's color settings. I also dont know ahead of time which programs may have been built with python (Azure CLI for example) to set the colors ahead of time.

Is there a way to change the DarkRed that python wants to use for errors to a color that is easier to distinguish, like "regular" Red? For any py script or program that runs on my machine?

enter image description here

EDIT: Here is an example of invoking a program written using Python and the dark red. My py scripts library is on my work computer. enter image description here

EDIT2: Its pip that was the other thing that uses the dark red. enter image description here

StingyJack
  • 19,041
  • 10
  • 63
  • 122
  • Can you give an example of how you run your Python script and what parts show up as red? When I run Python scripts from the command line (on Windows, both on cmd and PowerShell), error messages show up in the same text colour as the regular standard output. This appears to be the default, so perhaps something is specific to your setup that you're not aware of. – Grismar Feb 11 '19 at 04:03
  • 1
    @Grismar - added a screenshot. – StingyJack Feb 11 '19 at 04:08
  • This looks like a feature of your shell. – Klaus D. Feb 11 '19 at 04:12
  • The error message appears to be produced by, or at least written to the window by, the Azure CLI. Perhaps you can look into the configuration for this specific tool, but the issue is not with Python. I'd suggest updating the question tags to at least include `azure` and possibly remove `python` as pythonistas and pythoneers won't be of much help. – Grismar Feb 11 '19 at 04:16
  • @Grismar - Pretty sure I had this dark red when running a python program/script lib I've recently been tasked with maintaining. I'll verify tomorrow at work (and probably take your advice). – StingyJack Feb 11 '19 at 04:20
  • @KlausD.- my shell is set to use standard "Red" for errors. – StingyJack Feb 11 '19 at 04:21
  • @StingyJack in my experience, only the error messages of the shell itself show up in the standard error colours (and that would be bright red, not the dark red, as you showed). However, specific scripts or libraries may include code that causes the script to report errors in colour, or you may have a specific distribution of Python and you could be running your scripts through its front-end CLI instead of vanilla Python itself. Do come back if you have these issues, people here might be able to help with that. – Grismar Feb 11 '19 at 04:25
  • The other script/program was/is probably using this knack library as well. – StingyJack Feb 11 '19 at 11:55

2 Answers2

2

First, python is innocent. The culprit is azure-cli itself. It uses a lib named knack for configuring logging. And knack uses colorama to configure colored output.

But the problem is, the RED in colorama is \033[31m. Which is what you see, somehow like dim red.

So the solution is simple, we manually modify that knack package.

Suppose your azure-cli is installed at C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2.

  1. Then go to C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\Lib\site-packages, delete that knack directory or rename it.
  2. Go to https://github.com/Microsoft/knack, download the package. add one line at line 47:
class _CustomStreamHandler(logging.StreamHandler):
    COLOR_MAP = None

    @classmethod
    def get_color_wrapper(cls, level):
        if not cls.COLOR_MAP:
            import colorama

            def _color_wrapper(color_marker):
                def wrap_msg_with_color(msg):
                    return '{}{}{}'.format(color_marker, msg, colorama.Style.RESET_ALL)
                return wrap_msg_with_color

            colorama.Fore.RED = "\033[31;1m"  # <- add this line
            cls.COLOR_MAP = {
                logging.CRITICAL: _color_wrapper(colorama.Fore.RED),
                logging.ERROR: _color_wrapper(colorama.Fore.RED),
                logging.WARNING: _color_wrapper(colorama.Fore.YELLOW),
                logging.INFO: _color_wrapper(colorama.Fore.GREEN),
                logging.DEBUG: _color_wrapper(colorama.Fore.CYAN)
            }

        return cls.COLOR_MAP.get(level, None)
    ...
  1. Copy modifed package to corresponding location.
  2. Test it again.
  3. Bingbangba!
Sraw
  • 18,892
  • 11
  • 54
  • 87
  • Thanks, I just arrived at this block of code as well, but was thinking to use LIGHTRED_EX instead of just RED. Really its the OS console's color preferences that should be getting read here, but I'm quite the noob w/ Python. – StingyJack Feb 11 '19 at 11:53
  • @Sraw I downvoted your answer because you started it with "First, python is innocent." which is not true. This is an accessibility issue with Python executable, not only with azure-cli. Many people have various types and degrees of color blindness, and it is totally user-hostile not to provide an option to totally disable color output in this century. Python should be called out on it, not absolved from responsibility because users might be able to fix it themselves. – Igor Levicki Jun 01 '22 at 17:42
  • @IgorLevicki I am not sure about what you mean by "Python executable". Python itself is just a language. A language itself does not control display at all. As mentioned in the answer, `azure-cli` is controlling the display color, there is nothing a programming language can do here. It is somehow like writing unreadable sentences with a pen in English and saying "Why couldn't this pen or language be made to guarantee readable sentences?" – Sraw Jun 01 '22 at 21:04
  • @Sraw By python executable I mean just that -- `python.exe`. When I execute a python script in `cmd.exe` console window any errors are colored dark red on black background making them totally unreadable for me. There is no `azure-cli` installed, so what I am saying is that for me the dark red color is not coming from `azure-cli` but from the Python itself unless the official Python install for Windows is somehow incorporating `azure-cli`. – Igor Levicki Jun 08 '22 at 13:49
2

For pip specifically, there is a "--no-color" command line option: https://pip.pypa.io/en/stable/reference/pip/#general-options

Kilo
  • 558
  • 1
  • 5
  • 13