[Python] How to best change the colour of the Python prompt?

Discussão em 'Python' iniciado por Stack, Janeiro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have an alias for python3 and one for a Python calculator (pm for Python Math):

    alias p3='python3 -q'
    alias pm='python3 -qic"from math import *; import sys; sys.ps1=\"] \"; sys.ps2=\"… \""'


    Since Python 3.13, the CLI prompt is coloured (ANSI bold purple on my Mac, I don't know elsewhere). This applies also to my pm command, which means that the colour is not encoded as an ANSI sequence in sys.ps1 and sys.ps2 (docs). A help(sys) doesn't tell me where the colour(s) could be stored, and anyways, the same colour seems to apply to error and other messages, while the error location has a separate colour (bold red).

    The following works for only modifying the prompts (cyan 0;36 in this case) (see here for more ANSI codes, but \e→\033):

    alias pm='python3 -qic"from math import *; import sys; sys.ps1=\"\033[0;36m] \033[0m\"; sys.ps2=\"\033[0;36m… \033[0m\""'


    but I cannot do the same for my p3 command without losing the capability of using p3 for launching a script (because -c makes it exit on completion, while -i forces it to be interactive).

    Is there a "cleaner" way of changing the colour of the prompts (maybe without also changing the colour of error messages)?

    Continue reading...

Compartilhe esta Página