【问题标题】:zsh always show Python virtual envzsh 总是显示 Python 虚拟环境
【发布时间】:2022-08-18 00:08:54
【问题描述】:

我目前有这个脚本来显示我的 GitHub 分支和虚拟环境:

setopt PROMPT_SUBST
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle \':vcs_info:git:*\' formats \'(%b)\'
MYPS1=\'\'
MYPS1+=\'%F{green}\'
MYPS1+=\'${${(%):-%n}:0:1}\'
MYPS1+=\'@\'
MYPS1+=\'${${(%):-%m}:(-4)}\' # Get last 4 chars of var machine name
MYPS1+=\':\'
MYPS1+=\'%F{yellow}\'
MYPS1+=\'%1~\' # Show only the name of the working directory or ~ if it is the home directory
MYPS1+=\'%F{magenta}\'
MYPS1+=\'${vcs_info_msg_0_}\' # Show git branch if any
MYPS1+=\'%f\'
MYPS1+=\'%# \'
PS1=$MYPS1

有时我需要更新我的.zshrc,所以我运行:

source ~/.zshrc

问题是,每当我重新加载我的 shell 时,即使它仍然处于活动状态,我也无法再看到我的 Python 虚拟环境。

# After activating virtual env
(my-ve-3.7.13) u@m1:repo-name(github-branch)%
# After reloading my zsh
u@m1:repo-name(github-branch)%

我使用pyenvvirtualenvs

如何在提示符中保留虚拟环境名称?

  • 检查名为VIRTUAL_ENV_PROMPT 的变量并将其值添加到提示符(如果存在)。
  • 请记住,如果您在虚拟环境处于活动状态时对提示执行任何其他操作,deactivate 将简单地将提示替换为创建 venv 时的值,而不是仅从您定义的任何提示中删除 venv 的名称同时。

标签: python macos virtualenv zsh


【解决方案1】:

在@chepner 的评论之后,我想通了:

  • 使用env 查看所有环境变量的列表。 pyenv 使用 PYENV_VERSION
  • 将其添加到提示符中,使用()pyenv 具有相同的外观。
...
MYPS1=''
MYPS1+='($PYENV_VERSION) '
MYPS1+='%F{green}'
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 2020-11-19
    相关资源
    最近更新 更多