Upgrading to Python 3 (Apple)

During Learn To Code Camp, we will be using the Python programming language as well as some others.  Python is an interpreted language, which means that the code that a programmer writes is passed through a program that interprets it and translates it into instructions for the computer to follow.  Code in other languages, like C and Fortran, is compiled into sets of instructions that directly control your computer.

Python comes in two major versions: version 2 and version 3.  As you can guess, version 3 is the newer version and Version 2 is the older “legacy” version.  Unfortunately, since version 3 is not backwards compatible with version 2, you can write code using one version that is not runnable in the other and vice versa.  Although there are reasons to learn Python 2, for Learn To Code Camp, we will use Python 3 because Python 2 will eventually go away and we want your code to last.  So, if your computer doesn’t already have Python 3, you must install it.  Don’t worry though.  Your computer can handle having both versions installed on it.

If you have an Apple computer (Mac OS X), you probably already have Python 2.7 installed.  To check, open Terminal and type the following command:

python --version

The response will probably be “Python 2.7.10”.  Of course, that means that you have Python 2 installed as your default.  This doesn’t mean that Python 3 isn’t installed, though.  To see if it is there, type

python

with no space at the end followed by two tabs.  In Linux environments, including the Mac OS Terminal, typing a tab asks the operation system to “auto-complete” the command that you’ve started.  After the first tab, if the command that you started is unique, then you will see a space after your command (python in this case).  If it is not unique, the operating system will auto-complete to the point of ambiguity.  Then, the second tab tells the operating system to show you all the completion options.

If there are different Python versions installed, when you “double-tab” the other options (e.g., python3, python3.7) will be listed for you.  If no Python 3 option is presented, then none is installed.

Fortunately, installing Python 3 is easy.

  1. Visit the Python website to find the latest Mac OS X download: https://www.python.org/downloads/.
  2. Down load the latest installer package for Python 3.7.2 (current as of this writing).
  3. Open the installer package that you just downloaded.
  4. Follow the instructions in the installer.

That’s it!  Python 3 is installed now.  However, your default version is still Python 2.  As nice as it would be to upgrade to Python 3, doing so might render some installed application that depends on Python 2 unusable.

If you type “python3” in Terminal, you’ll start the Python interpreter.  Try it now.

$ python3
Python 3.7.2rc1 (v3.7.2rc1:75a402a217, Dec 11 2018, 18:05:25) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print( "Hello, World!" )
Hello, World!
>>> quit()
$

With that, you’re ready to go.