admin管理员组

文章数量:1419220

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)?

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)?

Share Improve this question edited Jan 29 at 21:25 wjandrea 33.3k10 gold badges69 silver badges98 bronze badges asked Jan 29 at 12:19 Walter TrossWalter Tross 12.7k3 gold badges42 silver badges69 bronze badges 7
  • 1 Coloring seems to be hardcoded in github/python/cpython/blob/main/Lib/_pyrepl/reader.py#L552 and that line also show how its done. If you can live with using _colorize module, maybe use it directly without having your own ansi sequences .. – rasjani Commented Jan 29 at 12:59
  • And for How to do that, for interactive shell, i'd set the prompt coloring to script that is referred in ${PYTHONSTARTUP} environment file. See: docs.python./3/using/cmdline.html#envvar-PYTHONSTARTUP – rasjani Commented Jan 29 at 13:16
  • @rasjani thanks for the link to the code, but I don't understand it, also because ps1 is not initialized to >>> and does not come from sys.ps1. Anyway, I would avoid using an otherwise unneeded module. Regarding PYTHONSTARTUP: you are right that this is the way to go to execute setup code only in interactive mode if -c is not desired because it make interactivity depend on -i. It's a pity that it needs an extra file (that, e.g., you must not fet to take with you when changing machine). – Walter Tross Commented Jan 29 at 15:07
  • Is there a reason you're going to all this effort instead of just setting the TERM variable to a terminal type that doesn't support color? That would make it as simple as python3() { TERM=dumb python3 "$@"; } (shell functions should always be preferred to aliases unless you're in one of the rare corner cases where you want to expand a command fragment that isn't valid syntax until expansion). – Charles Duffy Commented Jan 29 at 21:35
  • 1 I have my .pythonrc part of my "dotfiles" repository so when ever im on new machine that i actually control, i install my dotfiles and pythonrc is part of that. – rasjani Commented Jan 30 at 7:52
 |  Show 2 more comments

1 Answer 1

Reset to default 1

I have tested this on bash (i am on ubuntu using fish and bash) with python3.13

you initially need a pythonrc file in location: "~/.pythonrc.py" And you can set your genera settings here

import sys
sys.ps1 = "\033[0;36m] \033[0m"
sys.ps2 = "\033[0;36m… \033[0m"

Then in bashrc (zsh in your case ) add line

export PYTHONSTARTUP=~/.pythonrc.py

So you can separte the stylization from your alias and safely include this on all your aliases

Edit : to use diffent prompts in diffent aliasses

alias pyCoolSnakes='PY_MODE=coolSnake python _rest of alias_'
alias pyCoolSnakes='PY_MODE=test python _rest of alias_'

Then in your pythonrc

import os  

mode = os.getenv("PY_MODE", "default")  

if mode == "coolSnake":  
    print("my snake mode")  
sys.ps1 = "\033[1;36m

本文标签: How to best change the colour of the Python promptStack Overflow