DEV Community

Cover image for How to clean your MacOS from too many Python versions and install it the right way
Kamran Badirov
Kamran Badirov

Posted on

How to clean your MacOS from too many Python versions and install it the right way

Over the years of learning how to program I have installed too many Python libraries with different versions of Python. To make them ready to work immediately, I installed them blindly without control. At some point, it got so messy that I didn't even remember which one was for which project. In this article, I will provide a set of instructions on how to delete redundant libraries and versions of Python, and install them correctly.

If you installed python from different sources (i.e. python.org, pip, easy_instal, macports, homebrew, fink) then binaries and libraries can be found in different locations of your system. It happens because these package managers put packages to different locations, for example, mac_ports puts things into /opt/local/, while homebrew uses /usr/local/.

Step 1

to see all the installed python versions and their location open the Terminal and run:
whereis python3 and whereis python

these commands will also show the location of the binaries. To see the versions of all installed python run:

which -a python3 and which -a python

To see python versions installed by Homebrew run: brew list | grep python

Step 2

Now, when it comes to deleting all unnecessary python versions make sure NOT to delete Python versions from the following directories:

/usr/bin
/System/Library
Enter fullscreen mode Exit fullscreen mode

The Apple-provided build of Python is installed in /System/Library/Frameworks/Python.framework and /usr/bin/python, respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software. Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do. For more info refer here

Starting from MacOS 12.3 Apple removed Python2.7 from its OS, and in general, Python 2 is not supported anymore therefore it is worth considering switching to Python 3 if you haven't yet.

To delete Python versions installed with Homebrew run the following:

brew list python| xargs brew uninstall --force

To delete other python versions from your macOS run:

sudo rm -rf “/Applications/Python”
sudo rm -rf /Library/Frameworks/Python.framework
sudo rm -rf /usr/local/bin/python
Enter fullscreen mode Exit fullscreen mode

To see broken symbolic links:

ls -l /usr/local/bin | grep '../Library/Frameworks/Python.framework/Versions/[version number]'

To remove them:

cd /usr/local/bin

ls -l /usr/local/bin | grep '../Library/Frameworks/Python.framework/Versions/[version number]' | awk '{print $9}' | tr -d @ | xargs rm*

Enter fullscreen mode Exit fullscreen mode

If you haven't installed Homebrew I recommend doing so because it is useful (and very convenient) for both installing/removing packages (which we will do in the next step) and removing symlinks. After installing Homebrew run the following command to see broken symbolic links to Python binaries:

brew doctor

if any error is found just use brew prune and the error will be fixed:

brew prune

Step 3

Now that all redundant versions have been removed we can install Python the right way. There are many ways to install Python: anaconda, python.org, brew, etc. I recommend using brew because it always installs the latest version and is super easy to use. To install the latest version of python simply run:
brew install python

The above command install the latest stable version of Python3 together with pip. Pip is a package manager for Python packages specifically. If you intend to use different versions and libraries it is worth considering using a virtual environment. Using virtual environment will save you the hassle of having different python versions and packages all over your system as everything will be contained in a single environment. To do so simply run:

pip3 install virtualenv

To create a virtual environment:

virtualenv -p python3 <desired-path>

Activate the virtual environment:

source <desired-path>/bin/activate

Deactivate the virtualenv:

deactivate

Final Thoughts

Moving forward, be aware that different ways and tools install things independently to different locations, so use them mindfully. I recommend using brew as it makes it easy to install and update applications and utilities on a Mac.

Top comments (0)