Escaping the grasp of multiple Python3 versions

This article outlines the steps to install and manage different versions of python3 without bothering the default python2 on mac.

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:

  1. Firstly, disable pyenv’s control over which python to call by editing ~/.zshrc and removing the pyenv init line. This will remove the pyenv shims directory from PATH, and future invocations of python will execute the system Python version, as before pyenv.
  2. Next, to delete all the versions of python currently present under pyenv, run rm -rf $(pyenv root) .
  3. Finally, if you had installed pyenv using homebrew, uninstall it using brew uninstall pyenv .
  4. 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.

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.

This will install the version using dependencies from homebrew and finally tell you that it has been installed under .pyenv/versions/ .

  • I would like to set this version of python as my default under pyenv and verify it by:
  • 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):”

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.

On successful installation, it’ll prompt that loaction of the latest python3 as

  • 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):

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:

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.

Hakuna Matata!

Looking to expand my knowledge base and save them here

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store