I will assume a version of Max OSX of at least High Sierra (10.13 or later). Upgrades are free.
This document supersedes any conflicting instructions in the tutorial itself. As versions of Python and operating systems have changed, some old usage instructions get out date. I am keeping all the latest system information in this document.
I will use Python 3.7+ to mean the current version of Python, with version number at least 3.7.3. Make sure you have the latest recommended version installed from https://www.python.org/downloads/. Download the pkg file and double click to execute.
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.
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:
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 sub-folder. If you have a "comp 150" parent folder, you might rename it "comp150".
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.7+. If you can double click on a .py file in my examples, and Idle opens as soon as you have installed Python 3.7+, great - you should skip the rest of this section.
If Idle did not open, here is how to change the default behavior:
Options
You may select Open in the File menu inside Idle.
Caution: the behavior depends on what window is active at the moment - a file editing window or the Shell window:
If you have modified a file and want to save it under a new name, make sure the file is in the active window (the shell window is not active), 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). The default is to save the file as a Python file, ending in ".py". If that is not what you want, note the Format field at the bottom of the Save dialog and change it to All 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.
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. If you run the program from inside Idle,
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.
The administrators of my server changed the security parameters, so now the links to run cgi scripts on my server do not work. Just use the ones the the local server on your machine, as discussed below. Do not click on Ch4 links to URL's at anh.cs.luc.edu.
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.
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:
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 (with CGIfix.py)!
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.
To use the Hands-on 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.