Some Special Mac Instructions

I will assume a version of Max OSX of at least El Capitan (10.11 or later). Upgrades are free.

This document supercedes any conflicting instructions in the tutorial itself. As versions of Python and operating systems have changed, some old usage instructions get out date.

Versions

I will use Python 3.5+ to mean the current version of Python, with version number at least 3.5.1. Make sure you have the latest recommended version installed from https://www.python.org/downloads/. Download the pkg filke and double click to execute. As you install, note the warning about the graphics support package Tcl/Tk as described on https://www.python.org/download/mac/tcltk/

In particular, it probably says that El Capitan, OSX 10.11 users should go to the Active State website and install the package. The version suggested by Python.org, 8.5.18.0 has a link near the bottom of the page. I am not sure if the latest link, at the top, works.

Your Login ID

The normal place for you to have my examples folder is under your home folder, which has the same name as your Login ID. If you are just creating a login ID, it will save some hassle in Chapter 4, if your login ID does not have a space in it. "Andy" or "AndyHarrington" would be fine for me, but

"Andy Harrington"

would bomb the server in Chapter 4.

If You Have a Blank in your Login ID

The local server program central to chapter 4 bombs with folder paths with a blank in the name.

If you have a blank in your Login ID, it is not so easy to change. Instead make a folder outside of your home folder:

  1. Go to the Macintosh HD folder and create a folder (like "comp150") with no blank in it! You will need administrative privileges, and will be prompted to enter your password.
  2. I suggest that you move my examples folder, or just the subfolder www so it is under your new folder. For Chapter 4 test my examples and add your work there.

Folder For My Examples

Wherever you plan to unzip my Python examples folder from http://anh.cs.luc.edu/python/hands-on/3.1/examples.zip you need to make sure that my www folder, and any other folder where you put later .cgi files does not have a blank in its name, or any folder in which it is a subfolder. If you have a "comp 150" folder, you might rename it "comp150".

Running VS. Editing

Editing by Default

You will be working mostly with files ending in .py -- Python source files. Most of the time you will be editing them and running them in IDLE, so the best default behavior when opening a file with extension .py is to have it open in Idle 3.5+. If you can double click on a .py file in my examples, and that happens as soon as you have installed Python 3.5+, great. If not, here is how to change the default behavior:

  1. In the Finder go to a file in my examples folder with the .py extension. and right click or control click on it. Select Get Info.
  2. In the Info window that pops up, In the dropdown menu for Open with:, you want to see IDLE.app on the top line (meaning it is already the default). You may possibly also see the version listed if you have several. Presumably you are going through this because you do not see IDLE.app there.
  3. You may see IDLE.app lower down in the list of options. Then select it there.
  4. If you did not see IDLE.app in the dropdown list at all:
    • Select Other....
    • A window of app's pops up. Toward the bottom is a checkbox for "Always Open With". Check it.
    • If you do not see a Python folder in the list, just below the bottom of the page change Recommended Applications to All Applications
    • Now you should see a Python 3.5 foilder. Open it and select Idle.app.
  5. The Info window should become active again. Be sure that now under Open with: you see IDLE.app.
  6. Under the IDLE.app you should see a button, Change All.... So that you never have to go through all these steps again, be sure to click that button. A confirmation window will pop up. Select Continue.
  7. Now you can close the Info window, and you should be able to open all .py files directly in Idle for editing by double clicking in the Finder!

IDLE

Starting The Idle Shell

Options

  • Use Spotlight with idle.app. If there is more than one version of idle.app, make sure you choose one for Python 3.5+. 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 (more below).
  • You may open a Python file ending in .py into an Idle Edit window by selecting it directly in a Finder window if the defaults are set as discussed above. Instead you can Ctrl-click on the file, select Open With, and then choose Idle for Python 3.5+.

Opening Files from Inside Idle

You may select Open in the File menu inside Idle.

Caution: the behavior depends on whether a file editing window or the Shell window is active at the moment:

  • If the active window is some file editing window when you use the Idle File menu to open a file, your search will start in the same folder as the file you are editing (generally the desired behavior).
  • If you select File Open from the Shell window, the starting folder depends on how you started Idle:
    • If you started Idle from Spotlight or opened a file in the Finder, then your search will start in your Documents folder, probably not what you want.
    • If you started Idle from a terminal, you open to the directory where you gave the command to start Idle.

Saving Files

If you have modified a file and want to save it under a new name, make sure the file is in the active window (not the shell), go to the File menu and Save. It is easiest if you save all files in the same folder: my examples folder (except in Chapter 4).

Running Program Files

Make the Edit window for the file you want active by clicking in its window, 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

Remember this is the time when it is critical not to have .cgi files under a folder with a blank in the name. See the earlier discussion.

CGI Files on My Server Do Not Work

The administrators of my server changed the security parameters, so now the links to run cgi scriupts on my server do not work. Just use the ones the the local server on your machine, as discussed below.

Opening .cgi Files in Idle

By convention the server programs that you will be writing end in ".cgi". That is not an extension that is automatically associated with Idle for editing. You will want to change the association. Do it the same way as the instructions above for getting .py files to open in Idle by default, except choose a .cgi file in my www folder, and go through the same procedure.

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 that you run 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 (nor have several other technical things right).

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

Remember the www directory cannot have a directory name in the path down to that directory with a space in it. If you got that wrong, go back to the previous section.

In the finder open your www directory. You can open CGIfix.py in Idle and run it. Note the comment that the file cgiServerScript was created. You need that in the next section:

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, make sure it becomes executable (and possibly fix some other technical things) by launching CGIfix.py again.

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 first need to have launched the local CGI Server, however opening and running localCGIServer.py in Idle does not work!

Instead make sure all your files needed by the cgi server (.html, .cgi, localCGIServer.py) are in the same folder as the file created there by fixCGI.py: cgiServerScript. (Distinguish the separate file for Windows, startServer.cmd: Ignore that.)

In the Finder double click on cgiServerScript. This should start a window announcing the start of the CGI server. (Then you can leave it running for as long as you want to test .cgi files in the same folder.) Do not have several copies of the CGI server running at once!

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. There are a number of steps: be sure you carefully go through the list in the tutorial. Remember, html files calling a cgi file, and cgi files used directly are only run in your web browser with a URL starting with localhost:8080/. Otherwise nothing dynamic happens.

Terminal Use (Optional)

To use the Handson Python Tutorial, the information above should be sufficient to get your Mac usage going. Terminals are quite useful in other contexts: There are many things that can be done from such a window that cannot be done from the Finder or with an App.

If you would like a bit more background, read on.