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 | Show 2 more comments1 Answer
Reset to default 1I 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
版权声明:本文标题:How to best change the colour of the Python prompt? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1745298401a2652219.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ps1
is not initialized to>>>
and does not come fromsys.ps1
. Anyway, I would avoid using an otherwise unneeded module. RegardingPYTHONSTARTUP
: 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:07TERM
variable to a terminal type that doesn't support color? That would make it as simple aspython3() { 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