Move the eval $(pyenv init -) from zsh to xonsh

1.7k Views Asked by At

How can I move eval "$(pyenv init -)" that is in .zshrc to .xonshrc? What is the syntax in xonsh to do that?

2

There are 2 best solutions below

1
On

pyenv (at the moment) only supports POSIX compliant shells (like bash or zsh) as well as the fish shell. pyenv is not just a wrapper around python, it integrates itself into the running shell session in order to transparently provide the desired virtualenv.

eval "$(pyenv init -)"

takes the output of pyenv init - and runs (evaluates) it in the context of the running shell, just as if the output was written there instead of the eval command.

Having a look at the output of pyenv init - you can see, that it is a bit of shell code, that - among other things - defines the pyenv function.

export PATH="/home/adaephon/local/opt/pyenv/shims:${PATH}"
export PYENV_SHELL=zsh
source '/home/adaephon/local/opt/pyenv/libexec/../completions/pyenv.zsh'
command pyenv rehash 2>/dev/null
pyenv() {
  local command
  command="$1"
  if [ "$#" -gt 0 ]; then
    shift
  fi

  case "$command" in
  activate|deactivate|rehash|shell)
    eval "$(pyenv "sh-$command" "$@")";;
  *)
    command pyenv "$command" "$@";;
  esac
}

If run in a fish shell, pyenv init - returns code that does the same, but in fish's syntax.

-

For pyenv to work with xonsh it would have to output xonsh-compatible variable and function definitions. As far as I can see, you would have to at least edit the files libexec/pyenv-init and libexec/pyenv-sh-shell (and probably some plugins) for that.

1
On

pyenv init - generates a bit of bash code that can be sourced. xonsh has a way to source bash code: source-bash. Unfortunately, source-bash only takes a file as argument; it doesn't consume STDIN. The solution is fairly simple, though:

pyenv init - > /tmp/pyenv
source-bash /tmp/pyenv > /dev/null