Programming Environment: Mac OS X

This section will show you how to run your first Python program on an OS X computer.

Python on OS X

If you use OS X, you shouldn't have much trouble getting started with Python. Python is installed by default on OS X, so you can probably get started without modifying your system at all. But you might want to install a newer version of Python than what you already have installed, and this section can help you with that.

Installing Python

Python is probably already installed on your system. To find out if it is installed, open a terminal and type the word python. To open a terminal, go to Applications and look for Terminal, or enter 'terminal' in your system's search bar.

You'll probably see output that looks something like this:

$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
[GCC  4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>>

From this output, we can see that Python is installed, and the currently installed version is Python 2.7.6. You're now running a Python terminal session. You can start typing Python commands here, and you'll see your output immediately:

>>> print("Hello Python world!")
Hello Python world!
>>> 

Installing Python 3

There's a small chance Python 3 is already installed on your system. To find out, open a terminal and enter the command python3. You'll probably get an error message. If you find that Python 3 is already installed, you can skip ahead to the section about installing Sublime Text.

Installing Homebrew

Homebrew is a package manager for OS X, which makes it easy to set up a development environment on your system. Homebrew depends on Apple's xcode, so open a terminal and issue the following command:

$ xcode-select --install

This command will take a little while to run, depending on your connection speed. When it's finished, you can install Homebrew:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

This will install Homebrew, which is written in the Ruby programming language. Be careful when running commands like this; the -e flag tells your system to execute whatever follows. You should only execute code that you download from a trusted source. If you have any doubts about this, visit the Homebrew site yourself and follow the instructions there.

Once you have Homebrew installed, check its status and then install Python 3:

$ brew doctor
Your system is ready to brew.
$ brew install python3

Now you should be able to run Python 3 in a terminal session:

$ python3
>>> print("Hello Python 3 World!")
Hello Python 3 World!
>>>

To leave the Python session and return to a terminal prompt, enter Command-D.

top

Installing Sublime Text

Sublime Text is a simple text editor, which makes it easy to run Python programs. Output is displayed in an embedded terminal window, which allows you to easily see the results of your programs. Sublime Text is not free software, but it has a liberal trial policy. Try it out; if you like it, please purchase a license. Sublime Text is one of the few editors that's friendly enough to be perfectly appropriate for beginners, but fully-featured enough for many professionals as well.

To install Sublime Text, go to the Downloads page, and click on the OS X installer. Once it's installed, you should be ready to write Hello World, your first Python program.

top