Cannot switch Python with pyenv

[July 2021]
If you see this message when running eval "$(pyenv init -)"

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

you should check the message from pyenv init as the warning says, but in a nutshell, you can use eval "$(pyenv init --path)" instead.

And don't forget to accordingly update your ~/.bash_profile, ~/.zprofile, ~/.bashrc, ~/.zshrc or the like if necessary.ShareImprove this answerFollowedited Nov 22 '21 at 2:44answered Jul 2 '21 at 16:31

untidyhair2,09311 gold badge1111 silver badges77 bronze badges

  • 3thank you! in ubuntu a different file needs to be updated bashrcKZiovas Jul 9 '21 at 12:37
  • 21This solved my issue on Mac OS Catalina. I wasn't getting that warning, and my Python version wasn't updating despite having eval "$(pyenv init -)" in my .zshrc. Thanks! – SocraticDatum Jul 12 '21 at 21:47
  • 1I was NOT getting the warning, nor anybody in the class I was teaching, but was super frustrating ... Not happy that I never saw a headsup about this change. – fbence Oct 5 '21 at 14:54
  • 4For macOS Big Sur (installed via brew), I needed (as well as my existing eval "$(pyenv init -)" in .zshrc) to add the following to .zprofile: eval "$(pyenv init --path)". – aerospatiale Oct 22 '21 at 1:40
  • solved it on Big Sur ... a Big Thanks :) – msanjay Nov 29 '21 at 14:47Show 1 more comment

157

Try this: eval "$(pyenv init -)"

Example:

$ python -V
Python 2.7.9
mac:~ $ eval "$(pyenv init -)"
mac:~ $ python -V
Python 3.5.0

More info: https://github.com/pyenv/pyenvShareImprove this answerFollowedited Aug 20 '20 at 9:06

Jesse Aldridge7,44488 gold badges4444 silver badges7373 bronze badgesanswered Dec 8 '15 at 12:47

Diego Ubirajara1,71411 gold badge77 silver badges88 bronze badges

  • 8Per the documentation given, place eval "$(pyenv init -)" toward the end of .bash_profile. Note that for me, placing it in .bashrc did not work. (The docs warn about "strange" behavior if .bashrc is used.) – Rico Picone Sep 3 '18 at 4:05
  • 19Running eval "$(pyenv init -)" doesn't change the python -V in my case, even though I do have the pyenv versions installed in ~/.pyenvVadorequest Feb 25 '19 at 13:00
  • 63I was missing export PATH="$PYENV_ROOT/shims:$PATH" in .zshrc in addition of the export PATH="$PYENV_ROOT/bin:$PATH" I had configured – Vadorequest Feb 25 '19 at 13:06
  • 3As Vadorequest said, if pyenv is installed using homebrew, you have to add $PYENV_ROOT/shims to the path as $PYENV_ROOT/bin does not exist. I think that is unique to homebrew installs of pyenv. – majorgear Jul 13 '21 at 20:44
  • 17Not sure why, but on my system $PYENV_ROOT was never defined; adding export PATH="$(pyenv root)/shims:$PATH" to my .zshrc file worked. – Whatcould Sep 13 '21 at 15:38Show 4 more comments

48

For me, this worked on MacOS with ZSH after installing via Homebrew:

echo 'eval "$(pyenv init --path)"' >> ~/.zprofile

I initially had eval "$(pyenv init -)" inside of my ~/.zshrc file, but that didn't seem to work. After following this guide: https://github.com/pyenv/pyenv#basic-github-checkout I remved the eval call from the .zshrc file and added it to the .zprofile file using the above command and after restarting terminal everything worked.