Cannot switch Python with pyenv
I would like to use pyenv to switch python2 and python3.
I successfully downloaded python2 and python3 and pyenv with following codes.
brew install pyenv
brew install pyenv-virtualenv
pyenv install 2.7.10
pyenv install 3.5.0
However, I cannot switch from python2 to python3..
Soma-Suzuki:~ Soma$ python --version
Python 2.7.10
Soma-Suzuki:~ Soma$ pyenv global
2.7.10
Soma-Suzuki:~ Soma$ pyenv versions
system
* 2.7.10 (set by /Users/Soma/.pyenv/version)
3.5.0
Soma-Suzuki:~ Soma$ pyenv global 3.5.0
Soma-Suzuki:~ Soma$ pyenv global
3.5.0
Soma-Suzuki:~ Soma$ pyenv versions
system
2.7.10
* 3.5.0 (set by /Users/Soma/.pyenv/version)
Soma-Suzuki:~ Soma$ python --version
Python 2.7.10
Soma-Suzuki:~ Soma$
I do not understand why this happens.
For your information. My python is in this directory.
Soma-Suzuki:~ Soma$ which python
/usr/bin/python
Thank you in advance.
[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.
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/pyenv
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.
In ubuntu the ~/.bashrc
file needs to be updated and change eval "$(pyenv init -)"
to eval "$(pyenv init --path)"
You forgot to add this eval "$(pyenv init -)"
.
Add this to your .bash_profile or .bashrc file (mac <=10.14) or to your .zshrc file (mac 10.15+)