Update Python 2 to 3 or install Python 3 on Your Mac OS by using Homebrew

Your Macbook shipped with Python 2.7. These days you most project demand python 3.x. There are plenty of ways to do that. But when you are using the xcode you can face some problem like

ERROR: Command errored out with exit status 1: command: /Applications/Xcode.app/Contents/Developer/usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/py/7xl299ts7wd934sv_qchlym40000gp/T/pip-install-8jt2ko8y/pdftotext_2a85213f524e4e50a5b130bc2151df6f/setup.py'"'"'; __file__='"'"'/private/var/folders/py/7xl299ts7wd934sv_qchlym40000gp/T/pip-install-8jt2ko8y/pdftotext_2a85213f524e4e50a5b130bc2151df6f/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/py/7xl299ts7wd934sv_qchlym40000gp/T/pip-wheel-eh8s0nob cwd: /private/var/folders/py/7xl299ts7wd934sv_qchlym40000gp/T/pip-install-8jt2ko8y/pdftotext_2a85213f524e4e50a5b130bc2151df6f/ Complete output (14 lines):

1. Update existing Python version Safely in Catalina, BigSur:

With using zsh, I successfully updated the python version to 3.9

First, install the pyenv, just run

$ brew update
$ brew install pyenv

Another option just to clone the repository of pyenv

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Export the environment variable, run command one by one

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile

Just close the terminal and upon and execute the shell command

$ exec "$SHELL"

You can verify the installation and list the available python versions to install

$ pyenv install --list

The output will look like

Available versions:
2.1.3
2.2.3
2.3.7
2.4.0
2.4.1
2.4.2
....
....

Now run command to install required python versions, In our case, we are updating to python3.9

$ pyenv install 3.9

Run the following command to set the global version of python and verify the version

$ pyenv global 3.9
$ python3 --version

If you are interested to remove existing or install python version via Homebrew

2. Install Homebrew in Mac (Catalina, BigSur):

First, you need to install Homebrew on your Mac.

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

The second option is curl to install, if you are more careful just download the install.sh, file, and install manually.

$ curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh --output homebrew_installer.sh
$ more homebrew_installer.sh # review the script until you feel comfortable
$ bash homebrew_installer.sh

What is Homebrew:

Homebrew is open-source software package management, this discussion our main objective is not what is Homebrew. You can read more about Homebrew over the internet

Uninstall python 3:

Use the brew command to uninstall the python. I used an example for python3.7 just replace x with your current version.

Remove python3 with Homebrew:

brew remove python3.x

If it spits out an error like

Error: No available formula or cask with the name "python@3.x".

Remove python3 without Homebrew:

fist list the all python versions in shell, just run

$ ls -l /usr/bin/python*

It will show you all lists of python versions installed in the bin (binaries) directory.

lrwxr-xr-x  1 root  wheel      75 Jan  1  2020 /usr/bin/python -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel      82 Jan  1  2020 /usr/bin/python-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x  1 root  wheel      75 Jan  1  2020 /usr/bin/python2 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel      75 Jan  1  2020 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel      82 Jan  1  2020 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
-rwxr-xr-x  1 root  wheel  137552 Jan  1  2020 /usr/bin/python3
lrwxr-xr-x  1 root  wheel      76 Jan  1  2020 /usr/bin/pythonw -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
lrwxr-xr-x  1 root  wheel      76 Jan  1  2020 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7

To remove the right python version just run, In our case, we need to remove /usr/bin/python3

$ sudo rm /usr/bin/python3

if it shows the error “Operation not permitted”, try to follow the instructions open this link

If you still face permission issue just move forward

Remove pip3:

To find the installed place of pip3 just run

$ pip3 --version

The output will look like

$ pip 21.0.1 from /Library/Python/3.x/site-packages/pip (python 3.x)

to remove run,

$ sudo rm -rf /Library/Python/3.x/site-packages/pip/3.x 
$ rm /usr/local/bin/pip3.x  //if which pip3 shows

Install python with brew, just run

$ brew cleanup
$ brew install python3
$ brew install 

You can install pip with python file, and upgrade pip and setuptools

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python3 get-pip.py --force-reinstall
$ pip3 install --upgrade pip setuptools wheel

Happy Updating!