pyenv-virtualenv: zsh: command not found: pyenv

My .zshenv contains

export PYENV_ROOT="$HOME"/.pyenv
export PATH="$PYENV_ROOT"/bin:"$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

which works fine, however, if I simply add eval "$(pyenv virtualenv-init -)" I keep receiving the message zsh: command not found: pyenv

Pyenv and pyenv-virtualenv work perfectly fine and if I enter exec "$SHELL" the message goes away until I restart the terminal so I’m sure what the issue is.

I’m running Arch Linux, Zsh 5.4.2-1, tmux 2.5-3, termite 12-2, and the master branch of both pyenv and pyenv-virtualenv pulled today.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 36 (3 by maintainers)

Commits related to this issue

Most upvoted comments

I had this issue, after following https://github.com/pyenv/pyenv-installer

I changed my ~/.zshrc from:

export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

To

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

My guess is that pyenv is being installed in a location that zsh doesn’t use by default (like /usr/local/bin). You probably have a line at the top of your .zshrc that looks like export PATH=$HOME/bin:/usr/local/bin:$PATH. Moving that line to the top of your .zshenv might fix the problem for you.

In case of M1 users:

Pyenv gets installed to /opt/homebrew/Cellar/…

So running pyenv which pyenv showed me that I have pyenv installed to /opt/homebrew/Cellar/pyenv/2.0.6/libexec/pyenv.

Therefore, I added this to my .zprofile:

eval "$(/opt/homebrew/Cellar/pyenv/2.0.6/libexec/pyenv init --path)" instead of eval "$(pyenv init --path)"

Works as expected.

Found this issue after installing via the pyenv-installer. (zsh on wsl ubuntu running in conemu) At first I added:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

As mentioned in this thread. I got the following warning after restarting the shell:

WARNING: `pyenv init -` no longer sets PATH.
Run `pyenv init` to see the necessary changes to make to your configuration.

After running pyenv init, it pretty much told me to add eval "$pyenv init --path)" before the eval “$(pyenv init -)”` command. Which I did and now it works with the following:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

However this means, that the pyenv-installer is currently broken. 😦

I have the same issue. My ~/.zshenv file:

export PYENV_ROOT="$HOME"/.pyenv
export PATH="$PYENV_ROOT"/bin:"$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi
eval "$(pyenv virtualenv-init -)"

The above fix didn’t work for me though. I used the following block in ~/.zshrc instead:

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
  eval "$(pyenv virtualenv-init -)"
fi

This is my .profile and .zprofile settings which have solved the problem

  • .profile
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
  • .zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

It eval "$(pyenv virtualenv-init -)" is showing errors like “zsh: command not found: pyenv”, it indicates that pyenv-virtualenv-init was called before pyenv-init. Since pyenv-virtualenv is heavily depending on pyenv, it needs to be initialized after pyenv has initialized.

Pasting your full zsh configurations (regard to pyenv/pyenv-virtualenv) here could help further investigation.

My guess is that pyenv is being installed in a location that zsh doesn’t use by default (like /usr/local/bin). You probably have a line at the top of your .zshrc that looks like export PATH=$HOME/bin:/usr/local/bin:$PATH. Moving that line to the top of your .zshenv might fix the problem for you.

Works for me, thanks 😄

It works for me,I want to know why it works?

~ is not expanded within quotes.

i had also some difficulties in windows WSL2 & zsh on ubuntu 20.04 I’ve removed the export command and that fixed it for me, now it looks like this at the end of my .zshrc

PATH="$HOME/.pyenv/bin:$PATH"     
if command -v pyenv 1>/dev/null 2>&1; then
   eval "$(pyenv init -)"                  
fi