Time to Make the Switch

Upgrade your MacOS default from Python 2 to Python 3

2020 is now in full swing and it’s time to switch your default python if you haven’t done so already. Maintenance of Python 2.7 has stopped as of January 1st. This means that your current version of python is now legacy code! While keeping a version of Python 2 on your system may still be handy for older scripts, now is the time to update your system. In this briefing, we will download Python 3, make it the default and store Python 2 as an alias. Now to begin, let's explore our current Python environment.

Source [XKCD](https://xkcd.com/1987/)

To find where our default Python is located on the system, simply type:

which python

This will show you the path of the default Python. This may be informative if you have a default Python outside the /usr/local/bin system as I do. My default Python is a part of an Anaconda distribution. To see how to change the default Python in an Anaconda distribution, scroll down a bit past the default methods.

Default MacOS System

To see the current version of Python, simply type:

python --version

My current version is 2.7.15. To change the default version on a MacOS, we can begin by downloading the newest installer from the Python downloads page (https://www.python.org/downloads/mac-osx/). Downloading the installer will insert the new version into /usr/local/bin. Before we change the default Python, let’s link the old Python 2 version to python2 command.

echo "alias python2=/usr/local/bin/python2.7" >> ~/.bashrc

The alias “python2” means that when we type the command, the default python version will be our Python 2 distribution. Now let’s create an alias for the new Python 3.

echo "alias python=/usr/local/bin/python3.8" >> ~/.bashrc

That should do it. A limitation of this method is that you have to manually reset your alias if you want to update the Python version. Meaning for Python 3.9 you will have to re-do the prior command.

Upgrading with Anaconda Base

My system uses a default Python distribution apart of Anaconda. To change the default Python version, simply type:

conda install python=3.8.1

This will install Python 3.8.1. To check all available versions of Python on Anaconda, simply type:

conda search python

The installation of a newer version of Python may take several minutes and additional conflict resolutions as packages you have previously downloaded may not be compatible with the newer Python version.

There you have it! A simple walk through of updating your Python default so you’ll be ready for this decade! My name is Cody Glickman and you can find me on LinkedIn.

Comments

Popular posts from this blog

Qiime2 to Phyloseq

RpoB Processing