Currently prompt changing is working by default but I get the following warning:
pyenv-virtualenv: deactivate
pyenv-virtualenv: activate genius
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
I've set up my configuration like the following:
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
After activating a virtual environment I didn't get the warning anymore, but prompt didn't change as I expected. After changing the variable to export PYENV_VIRTUALENV_DISABLE_PROMPT=0 it also didn't produce any effect, still didn't get the original prompt behavior.
It's just expected. PYENV_VIRTUALENV_DISABLE_PROMPT will let pyenv-virtualenv to stop changing prompt. The prompt changing feature is just left for backward compatibility (only for non-fish users), and will be removed from future release. If there's any value in PYENV_VIRTUALENV_DISABLE_PROMPT, pyenv-virtualenv will not change shell's prompt anymore.
https://github.com/yyuu/pyenv-virtualenv/blob/v20160202/bin/pyenv-sh-activate#L215-L231
If you'd like to show current version in pyenv, please configure your shell by yourself with using something like https://github.com/yonchu/zsh-python-prompt
@yyuu Ah ok, I thought that was a warning to simulate the old behavior, not the new one.
Why did you decide to remove the prompt change feature? I personally like it, and will greatly miss it when it's gone. :(
@coredumperror and anyone else: I took a quick pass at making bash display the active pyenv python. As a bonus, it works for _any_ active version, not just virtualenvs. Maybe it will help you. Just put it in your bash startup scripts somewhere:
__pyenv_version_ps1 ()
{
local ret=$?;
if [ -n "${PYENV_VERSION}" ]; then
echo -n "(${PYENV_VERSION}) "
fi
return $?
}
PS1="\$(__pyenv_version_ps1)${PS1}"
According to the docs, PYENV_VERSION is only to be used when you want the active pyenv to be shell-wide, rather than application-specific (quite useful for containers, I imagine).
However, your code did inspire me to create a version that works for application-specific pyenvs:
__pyenv_version_ps1() {
local ret=$?;
output=$(pyenv local 2> /dev/null)
if [[ ! -z $output ]]; then
echo -n "($output)"
fi
return $ret;
}
PS1="\$(__pyenv_version_ps1)${PS1}"
stderr is redirected to /dev/null to silence the error when pyenv local runs in a folder with no local pyenv specified (no .python-version file). The output is then checked for being empty to avoid () appearing in the prompt.
@coredumperror
Good catch! Maybe a better line (so it doesn't only work with local) would be along the lines of: output=$(pyenv version-name). I _think_ that would work for all cases (but the system Python shows up as system. I kinda like it, but one might prefer to throw that value away).
Ah, I was unaware of the pyenv version-name command. That does seem like a more complete solution. However, it has a a major drawback: it decorates _every_ prompt. I personally only want my prompt decorated when I'm in a folder with a local pyenv configured.
To each their own, though. It's certainly the superior solution if you don't mind an always decorated prompt.
@coredumperror That's what I was referring to when I mentioned the system Python showing up as system. If you don't like that, change the condition to if [[ -n $output && $output != "system" ]].
Thank you for the clarification! I'm very new to bash programming, and that idea simply never occurred to me. Do note, however, that output = $(pyenv version-name) is syntactically invalid. It needs to be output=$(pyenv version-name).
This is probably a little hack-ish, but I adapted @mikenerone's initial function to use $PYENV_VIRTUAL_ENV instead. It's probably more likely to break in future, but the advantage is it should be snappier as it avoids forking a new process every time the prompt is evaluated:
__pyenv_version_ps1 ()
{
local ret=$?;
if [ -n "${PYENV_VIRTUAL_ENV}" ]; then
echo -n "(${PYENV_VIRTUAL_ENV##*/}) "
fi
return $?
}
Throw my $0.02 in the pile. I use bash, and I do not want to see prompt-changing go. It's too easy to get "lost" without it, and having to implement one yourself (or take some random solution from a comment somewhere) is a bit... crap.
Most helpful comment
Throw my $0.02 in the pile. I use bash, and I do not want to see prompt-changing go. It's too easy to get "lost" without it, and having to implement one yourself (or take some random solution from a comment somewhere) is a bit... crap.