I want to learn coding on my Mac - how do I set up Python, and where's the best place to learn Python coding?

Learning to code is hugely popular at the moment, and Python is a great coding language to learn. Luckily for us, the Mac is a great coding platform, and Python makes it easy to learn how to code on a Mac.

In this feature, we're going to look at setting up Python in OS X. It's very easy to explore Python on a Mac, and Python should be installed on your Mac already. You can test this out by opening Terminal and entering python --version. It should display Python 2.7.10 (you may have a slightly different version).

Want to read more about Mac coding? Learn to code using C on Mac | Learn to code in C# on MacLearn HTML & web design on Mac | Learn how to code in Java on Mac | Learn to code in SQL on Mac

How to set up and learn Python coding on a Mac: Install the latest version of Python 2

How to set up and learn Python coding on a Mac

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.

  1. Download the Mac OS X 64-bit/32-bit installer (not the PPC installer) from the Python website.
  2. Double-click the python-2.7.11-macosx10.6.pkg file in the Downloads folder.
  3. If you have Gatekeeper enabled, the installation will be blocked. Open System Preferences > Security & Privacy and click Open Anyway.
  4. 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.

Read next: Complete guide to coding and programming on a Mac

How to set up and learn Python coding on a Mac: Which version of Python should I use?

How to set up and learn Python coding on a Mac

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.

How to set up and learn Python coding on a Mac: 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.

  1. Open the App Store and search for Xcode.
  2. 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.

How to set up and learn Python coding on a Mac: Install Homebrew

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).

  1. Open a Terminal window
  2. Cut and paste this command: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

When it has finished installing, enter brew --version to check it's installed. And brew home to open the Homebrew webpage and learn how to use Homebrew.

Packages are installed using brew install. Unlike apt-get you don't need to use sudo with brew. (Don't worry if that previous sentence doesn't make sense! You'll get the hang of it as you start to learn Python.)

How to set up and learn Python coding on a Mac: Install PIP in OS X

PIP is a program used to install Python packages (these are pre-built blocks of code you'll be using inside your programs). Installing PIP is easy. Open Terminal and enter this line: sudo easy_install pip. Enter pip --version to make sure it's installed.

How to set up and learn Python coding on a Mac: The best Python text editors for Mac

How to set up and learn Python coding on a Mac: Text Wrangler for Mac

By default, Python files (ending in .py) will open in TextEdit, and this won't do. It has a nasty habit of a writing files in non-ASCII format, which messes things up. It also lacks any decent formatting options.

So you'll need to install a great text editor. Fortunately, there are a few available. Here are the three best text-editing applications for Mac.

  • TextWrangler: This app is a free download from the App Store. It's a lightweight version of BBEdit but has all the functionality you need. The best choice for starting out.
  • Geddit: This is a popular choice amongst Linux fans, so it's good for programmers to learn. Not as slick as other options, but very functional.
  • Sublime Text: This is my personal favourite. You can download and use it for free, although it'll occasionally nag you into paying for it. A slick interface that's easy on the eyes with great formatting control.

When you've installed your text editor, it'll be important to associate it with Python files (ending in "py".):

  1. Open the text editor and save a file called "test.py" to your desktop.
  2. Highlight the file and choose File > Get Info (Command-I).
  3. Change the option under Open With to your program of choice.
  4. Click Change All.

How to set up and learn Python coding on a Mac: Create a "Hello, World!" program

How to set up and learn Python coding on a Mac: Hello, World!

Now that you've got Python installed correctly, let's see how easy it is to create a Hello World program. "Hello, World!" is a standard first program that simply writes the words "Hello, World!" to the screen.

  1. Open Terminal.
  2. Enter touch helloworld.py to create a blank Python text file.
  3. Enter open helloworld.py. The file should open in the app you chose in the previous step.
  4. Enter a single line in the text file that reads: print "Hello, World!"
  5. Save the file (Command-S).
  6. Head back to Terminal and enter python helloworld.py.

Terminal should run the program and display "Hello, World!"

That is, at its most basic, how you create and run Python programs in a text edit app, and run them from the command line on a Mac. Of course, there's much more to programming in Python that that. And there are lots of courses out there ready to help you.

How to set up and learn Python coding on a Mac: The best Python training courses for Mac

How to set up and learn Python coding on a Mac: Python training course

Once you've got Python installed on your Mac, you'll want to start learning all the nuances of Python programming. Here are some of the best courses you can follow to learn Python programming.

  • Codecademy Python Course. This is a great website, although it teaches you how to use Python in a web browser, so don't forget to keep creating programs in your text edit app and running them for real in Terminal.
  • Learn Python the Hard Way. Don't be intimidated by the title. This course teaches you the nuts and bolts of programming.
  • Green Tea Press: Think Python. This free book takes you through different aspects of Python.
  • Udemy: Introduction to Python for Beginners. This is a paid-for course, although Udemy often has deals so keep your eye out. It's a good video tutorial that takes you through aspects of Python.

Read more: