Ignore this section if you are happy with your current install of pyenv.
To uninstall the existing pyenv, follow the steps below. The credit for this solution goes to the selected answer here:
- Firstly, disable pyenv’s control over which python to call by editing ~/.zshrc and removing the
pyenv initline. This will remove the pyenv shims directory from PATH, and future invocations of
pythonwill execute the system Python version, as before pyenv.
- Next, to delete all the versions of python currently present under pyenv, run
rm -rf $(pyenv root).
- Finally, if you had installed pyenv using homebrew, uninstall it using
brew uninstall pyenv.
- Also, ensure you have removed the pointer to these locations in PATH.
This part of the article helps you install pyenv, choose the python3 version you want, and install it. The main idea is taken from here. This article explains the idea really well and has helped me the last couple of years, whenever I have messed up my python installation.
To begin with, ensure
which python points to the default python2 on your mac. Next install pyenv using
brew install pyenv .
pyenv lets you easily switch between multiple versions of Python. For me the latest pyenv version is
pyenv 1.2.27 .
- Now let’s find out the available python3 versions and install the one we need.
pyenv install --list | grep " 3\."
Above command would grep the available python versions for 3.8 and 3.9. Change it according to your current requirement and availability. This gave me
3.9.4 as the latest version so I’ll be installing that. Note: in case you cannot find the latest version run
pyenv update to update the tool. Rerunning the above command should now give you the latest versions.
pyenv install 3.9.4
This will install the version using dependencies from homebrew and finally tell you that it has been installed under
- I would like to set this version of python as my default under pyenv and verify it by:
pyenv global 3.9.4
pyenv version #to verify
3.9.4 (set by /Users/dummy_user/.pyenv/version)
- As mentioned in the article I get these information from, “The power of pyenv comes from its control over our shell’s path. In order for it to work correctly, we need to add the following to our configuration file (.zshrc for me, possibly .bash_profile for you):”
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc
The above should help zsh to refer to pyenv’s default python3 version whenever you call python3.
NOTE: The one thing you should avoid doing is moving the python2 to a new location and placing your python3 folder in the same location to make it the default python. Although your OS will prevent you from doing this, but had it gone through, you’d have messed up the cases where your OS uses the default python2 in the system.
- Next let’s transfer the headache of updating python as a new version comes along, to homebrew.
brew list | grep python #will confirm you have python in your system
brew info python #will show you the current version you have
brew update && brew upgrade python #will update it to the latest
On successful installation, it’ll prompt that loaction of the latest python3 as
Python has been installed as /usr/local/bin/python3
- Finally, to be able to call this python3 you installed when you type
python, create a alias in .zshrc (for me, possibly .bash_profile for you):
echo "alias python=/usr/local/bin/python3" >> ~/.zshrc
Installation of the new python3 would have automatically updated your pip. Let’s also alias pip to the pip3 associated with this new homebrew-managed python version by:
echo "alias pip=/usr/local/bin/pip3" >> ~/.zshrc
exec $0#you should get the second line as output for the which below
pip: aliased to /usr/local/bin/pip3 python -m pip install --upgrade pip #to upgrade pip
python -m pip install --upgrade pip
Hope this helps in managing and updating python3 along with python2. If you want a more elaborate read on pyenv, refer to this article.