DEV Community

Cover image for # Right way to do a Python clean install on Mac
Aditya
Aditya

Posted on

# Right way to do a Python clean install on Mac

We all have been there trying to figure out the root cause of failure of your scripts, we finally decide to get ride of python once and for all (not really ;))

This is my opinionated post on how to clean python completely from your system and then do a fresh install


Use Homebrew to do the heavy lifting

check to see if homebrew is installed on your system. more info on its website -> https://brew.sh/

$ brew --version
Homebrew 2.5.11
Homebrew/homebrew-core (git revision d4069; last commit 2020-11-18)
Homebrew/homebrew-cask (git revision 0374c82; last commit 2020-11-18)
Enter fullscreen mode Exit fullscreen mode

If you don't see the above, lets install homebrew.

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

Now you should be able to run

$ brew --version
Enter fullscreen mode Exit fullscreen mode

Clean up existing Python installations

Note please dont remove the existing python 2.7 which comes along with your mac, there might be apps using this !

so the default installation of python exists at /usr/bin/python, lets keep it as is
However, the python installations located in /usr/local/bin are often symbolic links and can be deleted.

List all those by running

$ ls -l /usr/local/bin/ | grep python*
Enter fullscreen mode Exit fullscreen mode

You should now see a list of links which are most likely installed in this path -> ../../../Library/Frameworks/Python.framework. If Python were to be installed by Homebrew, it would be installed in the path -> /Cellar/python@...

Clean up all python links

$ sudo rm /usr/local/bin/python*
$ sudo rm /usr/local/bin/pip*
$ sudo rm /usr/local/bin/easy*
Enter fullscreen mode Exit fullscreen mode

Remove versions of python install in the Framework path

$ sudo rm -Rf /Library/Frameworks/Python.framework/Versions/*
Enter fullscreen mode Exit fullscreen mode

Environment setup

open ~/.bashrc and ~/.bash_profile and clean up all the references to python Framework path and only have

export PATH='/usr/local/bin:/usr/local/sbin:/usr/bin:$PATH'
Enter fullscreen mode Exit fullscreen mode

Install Python using brew

Using brew install python

$ brew install python3
Enter fullscreen mode Exit fullscreen mode

Note: as of Nov 17, 2020: the above command will install python@3.9

To always keep the python updated using brew

$ brew upgrade
Enter fullscreen mode Exit fullscreen mode

Hope this helps keep a clean and updated python version

๐Ÿป

Top comments (1)

Collapse
 
jorotenev profile image
Georgi Tenev

Isn't it risky to wipe out the system python interpretator? I used pyenv to manage multiple python versions on Mac and I'm happy with it