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
This is a great opportunity to learn about how pyenv works under the hood.
The pyenv global command simply reads the data in your /Users/Soma/.pyenv/version directory. It's basically the same as cat /Users/Soma/.pyenv/version.
The pyenv versions command is just checking through the hierarchy and selecting the right Python version to use when a "shim interceptable" command like python or pip is run.
When you run pyenv global 3.5.0, the /Users/Soma/.pyenv/version file is updated to contain "3.5.0". That's the only change pyenv makes. Most users are surprised that pyenv global 3.5.0 only changes a single line in a text file!
When you run python --version, your Terminal will perform the same steps it performs when any shell command is executed: it goes through each directory in your PATH and looks for the first executable named python.
If you type echo $PATH, you'll have something like this: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Your machine is finding the python executable in the /usr/bin directory.
You can add this code to your ~/.bash_profile file to change your PATH.
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
Restart your terminal, run echo $PATH again, and you'll now see output like this: /Users/Soma/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Notice how the /Users/Soma/.pyenv/shims directory is at the start of the PATH now. When you run python --version now, the command will be handled by the python executable in /Users/Soma/.pyenv/shims. The command won't have an opportunity to be picked up by /usr/bin/python because it'll be grabbed by /Users/Soma/.pyenv/shims/python first.
I can see why this bug confuses you. It's hard to debug this unless you know how pyenv works.