bash virtualenv 名称未显示在 zsh 提示中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38928717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
virtualenv name not show in zsh prompt
提问by OhMyGosh
Recently, I give a try on oh my zsh, everything looks good till I try virtualevn and virtualenvwrapper. When I activate a virtualenv (e.g test), on normal bash, I will see the virtualenv name like:
最近,我尝试了 oh my zsh,一切看起来都不错,直到我尝试了 virtualevn 和 virtualenvwrapper。当我激活 virtualenv(例如测试)时,在正常的 bash 上,我会看到 virtualenv 名称,如:
(test)abc@abc:
But when I switched to zsh, I cannot see virtualenv name. Even though, I alr add virtualenv and virtualenvwrapper in plugins of oh my zsh. I also checked the activate file of my virtualenv, it contains:
但是当我切换到 zsh 时,我看不到 virtualenv 名称。尽管如此,我还是在 oh my zsh 的插件中添加了 virtualenv 和 virtualenvwrapper。我还检查了我的 virtualenv 的激活文件,它包含:
f [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="$PS1"
if [ "x" != x ] ; then
PS1="$PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
fi
export PS1
fi
Is it because the comparision ["x" != x] return true?
是不是因为比较 ["x" != x] 返回 true?
Updated:I tried to echo $PS1 in activate file, and got this:
更新:我试图在激活文件中回显 $PS1,并得到这个:
(test) %{$fg[magenta]%}%n%{$reset_color%}%{$fg[cyan]%}@%{$reset_color%}%{$fg[yellow]%}%m%{$reset_color%}%{$fg[red]%}:%{$reset_color%}%{$fg[cyan]%}%0~%{$reset_color%}%{$fg[red]%}|%{$reset_color%}%{$fg[cyan]%}?%{$reset_color%}
It seems the $PS1 is correct, but when I echo $PS1 in the terminal, the (test) is gone. It seems the $PS1 is override by something else!
似乎 $PS1 是正确的,但是当我在终端中回显 $PS1 时,(测试)消失了。似乎 $PS1 被其他东西覆盖了!
采纳答案by OhMyGosh
Found the problem, it's due to the theme. The theme I used in the above case is pygmalion, it won't allow u to change $PS1.
发现问题了,是主题的原因。我在上述案例中使用的主题是 pygmalion,它不允许您更改 $PS1。
After changed to robbyrussell theme, I can change $PS1 in terminal, but still cannot see the virtualenv name. After a while debugging, I found that by default the virtualenv plugin of oh my zsh disable the prompt:
改成robbyrussell主题后,我可以在终端里改$PS1,但是还是看不到virtualenv的名字。经过一段时间的调试,发现oh my zsh的virtualenv插件默认禁用了提示:
# disables prompt mangling in virtual_env/bin/activate
export VIRTUAL_ENV_DISABLE_PROMPT=1
So just comment out the line in virtualenv plugin, problem solved.
所以只需注释掉 virtualenv 插件中的行,问题就解决了。
回答by William Entriken
Do this in ~/.zshrc
:
这样做~/.zshrc
:
plugins=(virtualenv)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status virtualenv)
Caveats:
注意事项:
1 -- add that plugin in addition to other plugins you have.
1 - 除了您拥有的其他插件之外,还添加该插件。
2 -- I'm using the POWERLEVEL9K theme. Maybe you theme
2 -- 我正在使用 POWERLEVEL9K 主题。也许你的主题
回答by Greg
The best solution is to add the following to the end of your ~/.zshrc file:
最好的解决方案是将以下内容添加到 ~/.zshrc 文件的末尾:
export VIRTUAL_ENV_DISABLE_PROMPT=
This will override the value in virtualenv.plugin.zsh - no need to change that file.
这将覆盖 virtualenv.plugin.zsh 中的值 - 无需更改该文件。
回答by Brian McCall
If you are using conda to start your virtual environment the envorionment variable will be different. To figure out the name of the environment that holds your virtaulenv name type printenv
and look through the output. For me it is CONDA_PROMPT_MODIFIER
如果您使用 conda 来启动虚拟环境,则环境变量会有所不同。找出保存您的 virtaulenv 名称类型的环境的名称printenv
并查看输出。对我来说是CONDA_PROMPT_MODIFIER
after you know the name of the variable open .zshrc and add this function
知道变量名称后打开 .zshrc 并添加此函数
function virtualenv_info {
[ $CONDA_PROMPT_MODIFIER ] && echo `basename $CONDA_PROMPT_MODIFIER`
}
function virtualenv_info {
[ $CONDA_PROMPT_MODIFIER ] && echo `basename $CONDA_PROMPT_MODIFIER`
}
and below that add this line
在下面添加这一行
PROMPT="%{$fg[green]%}$(virtualenv_info)%{$reset_color%}%${PROMPT}"
PROMPT="%{$fg[green]%}$(virtualenv_info)%{$reset_color%}%${PROMPT}"
close the editor and type source .zshrc
关闭编辑器并输入 source .zshrc
回答by cyber-math
My setting to display Python virtualenv name is the following.
我显示 Python virtualenv 名称的设置如下。
In the .zshrc
file:
在.zshrc
文件中:
1- virtualenv
added in plugins
1-virtualenv
加入plugins
2- Function for zsh
is added
2-zsh
添加了 功能
function virtualenv_info {
[ $VIRTUAL_ENV ] && echo ‘(‘`basename $VIRTUAL_ENV`') ‘
}
Navigate to your theme now
立即导航到您的主题
3- My theme is the default theme for zsh
.
3- 我的主题是zsh
.
`$ vim ~/.oh-my-zsh/themes/robbyrussell.zsh-theme`
4- Add this command right after existing PROMPT commands:
4- 在现有的 PROMPT 命令之后添加此命令:
PROMPT+='%{$fg[green]%}$(virtualenv_prompt_info)%{$reset_color%}%'
finally
最后
$ source ~/.zshrc
PS: you can add your name or a few space before or after the PROMPT+
PS:你可以在PROMPT+之前或之后添加你的名字或几个空格
Hope that helps!
希望有帮助!
回答by Yashashvi
As per this guide here
按照本指南这里
First add
virtualenv
dependency underplugin
in file.zshrc
If this doesn't work for you, then it means that the theme(one of oh-my-zsh theme) you have selected doesn't include virtualenv name in bash prompt so try second step.Go to file
~/.oh-my-zsh/themes/YOUR_THEME_NAME.zsh-theme
and add this in base prompt%{$fg[green]%}$(virtualenv_prompt_info)%{$reset_color%}%
首先在文件中添加
virtualenv
依赖项 如果这对您不起作用,则意味着您选择的主题(oh-my-zsh 主题之一)在 bash 提示中不包含 virtualenv 名称,因此请尝试第二步。plugin
.zshrc
转到文件
~/.oh-my-zsh/themes/YOUR_THEME_NAME.zsh-theme
并将其添加到基本提示中%{$fg[green]%}$(virtualenv_prompt_info)%{$reset_color%}%
NOTE: virtualenv_prompt_info
is the name of function which is declared in ~/.oh-my-zsh/plugins/virtualenv/virtualenv.plugin.zsh
. If your plugin file have different function name then change it accordingly.
注意:virtualenv_prompt_info
是在~/.oh-my-zsh/plugins/virtualenv/virtualenv.plugin.zsh
. 如果您的插件文件具有不同的函数名称,则相应地更改它。
Or you can declare your own function in ~/.zshrc
file as shown in this guide
或者您可以在~/.zshrc
文件中声明您自己的函数,如本指南所示
回答by Ilan Harel
In the case you installed Anaconda using Homebrew:
如果您使用 Homebrew 安装了 Anaconda:
brew tap homebrew/cask-versions
brew cask install anaconda
And you are using POWERLEVEL9K theme
并且您正在使用 POWERLEVEL9K 主题
git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k
All you need to do is add this line to the end of .zshrc :
您需要做的就是将此行添加到 .zshrc 的末尾:
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs history time anaconda)
There's no need for virtualenv plugin.
不需要 virtualenv 插件。
Edited:
编辑:
In case you already had conda installed for bash and you get:
如果您已经为 bash 安装了 conda 并且您得到:
zsh: command not found: conda
Run this:
运行这个:
~/anaconda3/bin/conda init zsh
回答by Labo
I made it work following this link: https://askubuntu.com/a/387098
我按照这个链接让它工作:https: //askubuntu.com/a/387098
I reproduce the answer below.
我在下面重现答案。
How the prompt is changed is defined in the script bin/activate
inside the virtual environment directory. This file is created by virtualenv
from a template. Unfortunatelly, the only way of prompt modification provided by the template is prepending (env name)
or whatever is set with --prompt
.
如何更改提示bin/activate
在虚拟环境目录内的脚本中定义。该文件是由virtualenv
模板创建的。不幸的是,模板提供的提示修改的唯一方法是前置(env name)
或任何设置为--prompt
.
To modify the prompt in the way you want, I'd suggest circumventing the setting of the prompt in bin/activate
and modify the definition of PROMPT
in your theme file.
要以您想要的方式修改提示,我建议绕过提示的设置bin/activate
并修改PROMPT
主题文件中的定义。
First add the following to your.zsh-theme
(or .zshrc
)
首先将以下内容添加到您的.zsh-theme
(或.zshrc
)
export VIRTUAL_ENV_DISABLE_PROMPT=yes
function virtenv_indicator {
if [[ -z $VIRTUAL_ENV ]] then
psvar[1]=''
else
psvar[1]=${VIRTUAL_ENV##*/}
fi
}
add-zsh-hook precmd virtenv_indicator
and add %(1V.(%1v).)
in front of the second line of the definition of PROMPT
. It should then look like this:
并%(1V.(%1v).)
在定义的第二行前面添加PROMPT
。它应该看起来像这样:
PROMPT='
%(1V.(%1v).)%{$fg_bold[grey]%}[%{$reset_color%}%{$fg_bold[${host_color}]%}%n@%m%{$reset_color%}%{$fg_bold[grey]%}]%{$reset_color%} %{$fg_bold[blue]%}%10c%{$reset_color%} $(git_prompt_info) $(git_remote_status)
%{$fg_bold[cyan]%}?%{$reset_color%} '
If you want some color you could add %(1V.%{$fs_bold[yellow]%}(%1v)%{$reset_color%}.)
for example.
如果你想要一些颜色,你可以添加%(1V.%{$fs_bold[yellow]%}(%1v)%{$reset_color%}.)
例如。
Explanation:
说明:
virtenv_indicator
will be called each time before the prompt is created. It checks if $VIRTUAL_ENV
is set and not empty. If so, it sets the first element of the $psvar
array to $VIRTUAL_ENV
with everything before and including the last /
removed (like basename $VIRTUAL_ENV
but less expensive)
virtenv_indicator
将在每次创建提示之前调用。它检查是否$VIRTUAL_ENV
已设置且不为空。如果是这样,它将$psvar
数组的第一个元素设置为$VIRTUAL_ENV
之前的所有元素,包括最后一个/
删除的元素(类似basename $VIRTUAL_ENV
但更便宜)
In the definition of PROMPT
%(1V.(%1v).)
checks if the first element of $psvar
is set and not empty (%(1V.true-text.false-text)
) and adds the content of the this element plus some parentheses ((%1v)
)
在定义中PROMPT
%(1V.(%1v).)
检查是否设置了第一个元素$psvar
且不为空 ( %(1V.true-text.false-text)
) 并添加 this 元素的内容加上一些括号 ( (%1v)
)
export VIRTUAL_ENV_DISABLE_PROMPT=yes
disables any prompt setting by bin/activate
scripts.
export VIRTUAL_ENV_DISABLE_PROMPT=yes
禁用bin/activate
脚本的任何提示设置。