2

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

Alexey Shrub
  • 1,216
  • 13
  • 22
redeemefy
  • 4,521
  • 6
  • 36
  • 51
  • you should post some context, eg add a snippet of your .bashrc containing the line. This appears to not be a ubiquitous setting. – Calvin Taylor May 30 '17 at 17:08

2 Answers2

1

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.

Adaephon
  • 16,929
  • 1
  • 54
  • 71
  • This is the correct answer, I think. Alternatively, xonsh vox plugin would need to implement the features of pyenv to enable installing and using alternative interpreters. As it is now, it creates a venv using the interpreter that it is itself running under (it's in dist-packages). I love xonsh and wish vox had this feature. As it is, if you just *must* switch between multiple python versions, a bash shell with pyenv is basically the only sane way to do it. – Joseph8th Dec 23 '18 at 04:09
1

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
  • Doesn't it depend on the shell you run these commands in? If you run this - like the question asker -- in the fish shell the output is fish-, not bash-compatible, right? – halloleo Oct 01 '19 at 06:42