Setting up the PythonLearn Environment on a Macintosh

Python is already installed on Macintosh OS/X operating system so all you need to add is a programmer text editor.

Pre-Requisite: TextWrangler

Please download and install TextWrangler from this site.

Important: Before you create your first program, you need to make one small change in the Preferences for TextWrangler. This will save you lots of "Python indent errors" anguish later. Under TextWrangler -> Preferences -> Editor Defaults tick the check box for "Expand Tabs" and close the dialog box (Screenshot).

Editing With TextWrangler

Note: We have a screen cast for the use of TextWrangler. You can either view this on YouTube or you can download the high-quality QuickTime version of the screen cast. You will need Apple QuickTime installed to view this video.

Go into the upper-right of your screen and click on the Spotlight search button and type textwrangler.

Then create your first Python program.

    print "Hello World"

Save your program as onto your Desktop. You will notice that after you save the file, TextWrangler will color your code based on the Python syntax rules. Syntax coloring is a very helpful feature as it gives you visual feedback about your program and can help you track down syntax errors more easily. TextWrangler only knows that your file is a Python file after you save it with a ".py" suffix.

Running Python Using the Built-In TextWrangler Shortcut

TextWrangler has a built-in way to run a Python program directly from the TextWrangler user interface. In TextWrangler go to the menu item #! -> Run in Terminal

TextWrangler will bring up a terminal window and automatically execute Python on your program. You will have to look closely to pick out the output from your program amongst the rest of the output in the terminal window.

In general, you can use the TextWrangler shortcut for very simple programs but once you start reading and writing files in your Python programs you should open and use the terminal program so you know what directory your program is running in - so you can open and read files.

Starting Terminal on Macintosh OS/X

The Terminal program on Macintosh is kind of buried under Macintosh HD -> Applications -> Utilities -> Terminal

There are several shortcuts that you might find helpful. You can go into the upper-right of your screen and click on the Spotlight search button and type terminal and you can execute Terminal from the pop-up list of items.

You can get Terminal to stay in your dock once terminal is launched by clicking and holding on the Terminal icon in the dock and then selecting Keep in Dock. Then you can quickly launch Terminal by clicking on the icon in the dock.

Where Are You?

When the command line starts up, you are in your "home" directory. In each of these examples, your logged in account should be used instead of csev.

    Macintosh Home Directory: 		/Users/csev
The command line prompt usually includes some clue as to where you are at in the folder structure on your hard drive. If you want to really figure out where you are, on Macintosh use the pwd command.
    udhcp-macvpn-624:~ csev$ pwd
    udhcp-macvpn-624:~ csev$ 

Where can you go?

Generally the first thing you want to do when you open a command line interface is to navigate to the right folder. Say you wanted to run a file from your desktop. The command is cd Desktop

    udhcp-macvpn-624:~ csev$ pwd
    udhcp-macvpn-624:~ csev$ cd Desktop
    udhcp-macvpn-624:Desktop csev$ pwd
    udhcp-macvpn-624:Desktop csev$
Nifty Trick: On the cd command, you can partially type a folder name like Desktop and then press the TAB key and the system will auto-complete the folder name if you have typed enough that the system can accurately guess what you mean to type.

Going Backwards (or Upwards)

You can change directory to the parent folder (the folder "above" the folder you are in using the cd .. command. It just says "go up one".

    udhcp-macvpn-624:Desktop csev$ pwd
    udhcp-macvpn-624:Desktop csev$ cd ..
    udhcp-macvpn-624:~ csev$ pwd
    udhcp-macvpn-624:~ csev$ 
If you get Lost...

If you can't figure out what folder you are in and/or cannot figure out how to get to the folder you want to get to "home" simply close and re-open the Command Line / Terminal window.

What Files/Folders are Here?

You can list the contents of the current directory using the ls -l command.

    udhcp-macvpn-624:Desktop csev$ pwd
    udhcp-macvpn-624:Desktop csev$ ls -l 
    total 8
    -rw-r--r--  1 csev  staff   15 Sep 16 15:17
    udhcp-macvpn-624:Desktop csev$ 
Running Your Python Program in the Terminal

Start the Terminal program, navigate to the proper directory and type the following command:

This loads the Python interpreter and runs, showing the program output and/or errors in the Terminal window.

Some Cool Hints on The Macintosh Terminal Program

You can scroll back through previous commands by pressing the up and down arrows and re-execute commands using the enter key. This can save a lot of typing. If you like keeping your screen uncluttered, you can clear the scroll back buffer by pressing the Command key and the K key at the same time.