How to set up and learn Python coding on a Mac: Install the latest version of Python 2
Python comes installed along with OS X, although you can make sure you have the latest version of Python by installing the current build from the Python website.
- Download the Mac OS X 64-bit/32-bit installer (not the PPC installer) from the Python website.
- Double-click the python-2.7.11-macosx10.6.pkg file in the Downloads folder.
- If you have Gatekeeper enabled, the installation will be blocked. Open System Preferences > Security & Privacy and click Open Anyway.
- Click Continue, Agree and Install buttons in the Install Python window.
Open Terminal again and enter python --version. It should now read Python 2.7.11.
Which version of Python should I use?
One confusing aspect of Python is that there are two major versions available, Python 2 and Python 3 (each with several sub-versions). What's the difference between Python 2 and Python 3?
Python 3 is the newer version of Python, so you can be forgiven for assuming that it's the best version. "Best" is pretty relative, though, and although Python 3 introduces a lot of new features, it also introduced a lot of incompatibilities.
Because coding in Python relies on using libraries of code developed by other people, this isn't a good thing.
Also, we've found that the major learning programs, from MIT to Codecademy, all use Python 2 to explain coding. These programs won't always work if you're using Python 3. So until you know what you're doing, stick with Python 2.
Install Xcode and Command Line Tools
Now that you have Python installed you should install Xcode and the Command Line Tools. Command Line Tools install a GCC compiler, and a lot of Unix-based tools also need a GCC compiler, so this is the easiest way to get one. Although you won't use Xcode to create Python programs, it'll make life easier down the line. Follow these steps to install Xcode and the Command Line Tools.
- Open the App Store and search for Xcode.
- Click Get > Install.
The Xcode Command Line Tools are installed alongside Xcode. Test that they're installed by entering xcode-select -p. You should see /Library/Developer/CommandLineTools.
Now that we've got Python and the Command Line Tools up and running, we should get Homebrew installed. Homebrew is a program used to install packages in OS X (it's the equivalent of apt-get, a command common on Unix and Linux computers).
- Open a Terminal window
- Cut and paste this command: /usr/bin/ruby -e "$(curl -fsSLhttps://raw.githubusercontent.com/Homebrew/install/master/install)"
Sign up for CIO Asia eNewsletters.