Some Special Mac Instructions

Your Login ID

It is important that your login ID not have a space in it. "Andy" or "AndyHarrington" is fine. "Andy Harrington" would bomb a bunch of things with no obvious explanation.

If you do have a login ID with a blank in it, you need to create a new user without a blank in the ID, and then use that ID.

IDLE

Starting The Idle Shell

Options

  • Use Spotlight with idle.app. If there is more than one version, make sure you choose one for Python 3.2+. This is a good approach the first time to check that Idle is properly installed. The disadvantage of this approach is that Idle starts in your Documents folder.
  • You may open a Python file ending in .py or .txt into an Idle Edit window by selecting it directly in a Finder window. If this does not work on your machine, then you have different defaults. Instead you can Ctrl-click on the file, select Open With, and then choose Idle for Python 3+.
  • You can also open Idle from inside a terminal, discussed more below under Chapter 4.

Opening Files from Inside Idle

You may select Open in the File menu inside Idle. Caution: If you use the Idle File menu when the Shell window is active, your search will start in your Documents folder, but if some file editing window is active instead, your search will start in its folder (generally the desired behavior).

Saving Files

If you have modified a file and want to save it under a new name, go to the File menu and use Save Copy As, NOT Save As. The latter saves a copy of the Shell history, not the program you are editing. It is easiest if you save all files in the same folder: my examples folder.

Running Program Files

Make the Edit window for the file you want active, then go to the Run menu and select Run Module. Note the F5 shortcut key.

Running Graphics (Chapter 2)

The graphics window likely comes up behind an unneeded Console Window. You can close the console window, and click on the graphics window title bar to bring it to the front.

Chapter 4 CGI Instructions

There are two setup issues to check, and then general instructions for dealing with individual cgi programs.

Setup: Finding Python3 And Idle3

When scrips are directly called by the operating system, they look for the proper interpreter to read them. Our scripts are set up to look for python3.

Open a terminal window (spotlight: Terminal)

Enter

python3

If a Python shell starts, fine. Just press Crtl-D once to quit, and you have found python3. If the shell did not start, you need the extra steps below.

Also try

idle3

If idle3 or python3 did not start, you will need to follow these instructions:

First check the location of the system Python files. If you have Python 3.2, try this terminal command:

ls /Library/Frameworks/Python.framework/Versions/3.2/bin

If you have Python 3.3, 3.4, ..., replace the 3.2. If your command does not cause an error, and shows python3 and idle3 listed, enter the following terminal commands (with 3.2 replaced if appropriate):

cd /usr/local/bin

If python3 was not found copy and enter:

sudo ln -s /Library/Frameworks/Python.framework/Versions/3.2/bin/python3 .

You may be asked for your password. You may get no feedback as you type your password.

Now test: Again try the command

python3

It should work now. End the command by entering Ctrl-D.

If you could not find python3, get help.

If idle3 did not work, enter a similar line:

sudo ln -s /Library/Frameworks/Python.framework/Versions/3.2/bin/idle3 .

Now test, starting idle3 from the command line.

If the idle3 command does not work, get help.

Idle From a Terminal - Opening CGI Files

There is one annoying feature of Idle on a Mac: The Open File dialog inside Idle only allows you to open files ending in .py or .txt, but not .cgi. Before chapter 4 this is not generally an issue. A way to get an existing CGI file into Idle:

  • cd into the folder of the file
  • enter the command idle3 followed by the file name.

For example you could edit and modify adder.cgi if you change to the www folder and enter

idle3 adder.cgi

Setup: Making CGI scripts executable

Make sure you have completed the previous section successfully before going on to this section.

A complication on a Mac, like any Unix derived system, is that programs must be explicitly marked executable. (On Windows it follows from the file extension, like .exe.) The examples/www folder may not have the cgi files marked executable.

The example program examples/www/CGIfix.py is needed to give direct Unix/Mac/Linux executability to CGI files for Chapter 4 and for any Python program in general.

Remember the www directory cannot have a directory name in the path to that directory with a space in it. If you got that wrong, move your files or change the name of the offending folder if possible.

Change into your www directory. For example if the example folder is directly in your desktop folder as in the example above, then enter:

cd ~/desktop/examples/www

Now make important files executable with the command:

python3 CGIfix.py CGIfix.py localCGIServer.py

(Note CGIfix.py is entered twice, once as the program to run and once as a parameter to that program!)

Finish with the command:

./CGIfix.py

This should make all the example cgi scripts executable.

Running CGI Scripts

Important! Particularly if you later copy in a CGI script from a Windows machine, or if you create any new cgi script in the www directory, run

./CGIfix.py

from the www directory again. You might want to just leave a terminal window open in your www directory.

If you forget this, and the file is not executable, nothing happens in the browser when you try to run it, and the error message in the server window is very unhelpful - it says "... File not found ...". Make sure you make new CGI files executable!

If you create and edit a cgi file inside Idle, remember you cannot run it from inside Idle. After editing, be sure to check the syntax, using alt/option-X. You should close the file in Idle before running ./CGIfix.py.

When you want to test a cgi script, you need localCGIServer.py running from the www directory. Again, assuming for illustration that you put the examples folder on your desktop, you could type in the command:

cd ~/desktop/examples/www

Here is a neat general alternate way to get to a folder:

  • Type cd and a space and stop - no return yet.
  • Find the www folder in the Finder and drag the www folder to the terminal: The full path should appear.
  • Press return to execute the change of directory.

Once in the www directory enter

./localCGIServer.py

You can just leave the server running. If you want to stop it, you can press Ctrl-C, or close its terminal window.

At this point you can do all the web server based activities in Chapter 4, with the only extra step being the running of ./CGIfix.py when you create a new CGI script in the www directory, or copy one from Windows. Remember, use your web browser with a URL starting with localhost:8080/