blob: d3b5e8bf2173e57fba4d919eff8b246647cffc5b [file] [log] [blame]
Jack Jansen5f962c21996-04-10 14:52:59 +00001<HTML>
2<HEAD>
3<TITLE>Using Python on the Macintosh</TITLE>
4</HEAD>
5<BODY>
6<H1>Using Python on the Macintosh</H1>
Jack Jansen5f962c21996-04-10 14:52:59 +00007<HR>
8
Jack Jansenbd9565a1996-04-15 12:25:44 +00009This document is an introduction to using Python on the Apple
10Macintosh. It does not introduce the language itself, for this you
11should refer to the <A
12HREF="http://www.python.org/doc/tut/tut.html">Python Tutorial</A> by
13Guido van Rossum. This guide more-or-less replaces chapter two of the
14tutorial, and provides some additional material. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +000015
Jack Jansen08365421996-04-19 15:56:08 +000016There is currently no good tutorial for the mac-specific features of
17Python, but to whet your appetite: it has interfaces to many MacOS
18toolboxes (quickdraw, sound, quicktime, open scripting, etc) and
19various portable toolboxes are available too (Tk, stdwin, complex
20numbers, image manipulation, etc). Some <A HREF="index.html">
21annotated sample programs</A> are available to give you an idea of
22Python's power. <P>
23
Jack Jansenbd9565a1996-04-15 12:25:44 +000024The document refers to Python 1.3.3 or higher, some of the features
25(like setting applet options) will not work in earlier versions of
26Python. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +000027
28<h2>Invoking the interpreter</h2>
29
Jack Jansenbd9565a1996-04-15 12:25:44 +000030The name of the interpreter may differ on different installations: it
31may be called <CODE>Python</CODE>, <CODE>PythonPPC</CODE> (for powerpc
32macs) or <CODE>Python68K</CODE> (indeed, for 68K macs). It will always
33be recognizable by the "16 ton" icon, though. You start the
Jack Jansen08365421996-04-19 15:56:08 +000034interpreter in interactive mode by double-clicking its icon: <p>
Jack Jansen5f962c21996-04-10 14:52:59 +000035
36<img src="html.icons/python.gif"><p>
37
Jack Jansenbd9565a1996-04-15 12:25:44 +000038This should give you a text window with an informative version string
39and a prompt, something like the following:
Jack Jansen5f962c21996-04-10 14:52:59 +000040<PRE>
41Python 1.3.3 (Apr 7 1996) [CW PPC w/GUSI]
42Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
43&gt;&gt;&gt;
44</PRE>
Jack Jansenbd9565a1996-04-15 12:25:44 +000045The version string tells you the version of Python, whether it was
46built for PPC or 68K macs and possibly some options used to build the
47interpreter. If you find a bug or have a question about how the
48interpreter works it is a good idea to include the version information
49in your message. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +000050
Jack Jansenbd9565a1996-04-15 12:25:44 +000051At the prompt you can type interactive python commands. See the
52tutorial for more information. The interactive window works
53more-or-less like a Communication Toolbox or Telnet window: you type
54commands at the bottom and terminate them with the <EM>[return]</EM>
55or <EM>[enter]</EM> key. Interpreter feedback also appears at the
56bottom of the window, and the contents scroll as output is added. You
57can use copy and paste in the normal way, but be sure to paste only at
58the bottom of the document.
Jack Jansen5f962c21996-04-10 14:52:59 +000059
60<h2>Creating Python scripts</h2>
61
Jack Jansenbd9565a1996-04-15 12:25:44 +000062The Python interpreter works in a way that is different from what you
63would expect of a macintosh program: the interpreter is just that: an
64interpreter. There is no builtin editor or other development
65support. Hence, to create a Python script you need an external text
66editor. For a first script you can use any editor that can create
67plain, unstyled text files, such as <CODE>SimpleText</CODE>. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +000068
Jack Jansenbd9565a1996-04-15 12:25:44 +000069For more serious scripts, though, it is advisable to use a programmers
70editor, such as <CODE>BBEdit</CODE> or <CODE>Alpha</CODE>. BBEdit is
71my favorite: it comes in a commercial version but also in a
72fully-functional free version <CODE>BBEdit Lite</CODE>. You can
73download it from the <A HREF="http://www.barebones.com/">BareBones</A>
74site. The free version will probably provide all the functionality
75you will ever need. Besides the standard edit facilities it has
76multi-file searches and many other goodies that can be very handy when
77editing programs. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +000078
Jack Jansenbd9565a1996-04-15 12:25:44 +000079After you have created your script in the editor of your choice you
80drop it on the interpreter. This will start the interpreter executing
81the script, again with a console window in which the output appears
82and in which you can type input if the script requires it. Normally
83the interpreter will close the window and quit as soon as the script
84is done executing, see below under <A HREF="#startup">startup
85options</A> for a way to change this. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +000086
Jack Jansenbd9565a1996-04-15 12:25:44 +000087It is a good idea to have the names of all your scripts end in
88<CODE>.py</CODE>. While this is not necessary for standalone scripts
89it is needed for modules, and it is probably a good idea to start the
90habit now. <p>
91
92If you do not like to start the Python interpreter afresh for each
93edit-run cycle you can use the <CODE>import</CODE> statement and
94<CODE>reload()</CODE> function to speed things up in some cases. Here
95is Guido's original comment for how to do this, from the 1.1 release
96notes: <P>
97
Jack Jansen08365421996-04-19 15:56:08 +000098<BLOCKQUOTE>
Jack Jansenbd9565a1996-04-15 12:25:44 +000099
100Make sure the program is a module file (filename must be a Python
101identifier followed by '<CODE>.py</CODE>'). You can then import it
102when you test it for the first time. There are now three
103possibilities: it contains a syntax error; it gets a runtime error
104(unhandled exception); or it runs OK but gives wrong results. (If it
105gives correct results, you are done testing and don't need to read the
106rest of this paragraph. :-) Note that the following is not
107Mac-specific -- it's just that on UNIX it's easier to restart the
108entire script so it's rarely useful. <P>
109
110Recovery from a syntax error is easy: edit the file and import it
111again. <P>
112
113Recovery from wrong output is almost as easy: edit the file and,
114instead of importing it, call the function <CODE>reload()</CODE> with
115the module name as argument (e.g., if your module is called
116<CODE>foo</CODE>, type <CODE>reload(foo)</CODE>). <P>
117
118Recovery from an exception is trickier. Once the syntax is correct, a
119'module' entry is placed in an internal table, and following import
120statements will not re-read the file, even if the module's
121initialization terminated with an error (one reason why this is done
122is so that mutually recursive modules are initialized only once). You
123must therefore force re-reading the module with <CODE>reload()</CODE>,
124however, if this happens the first time you try to import the module,
125the import statement itself has not completed, and your workspace does
126not know the module name (even though the internal table of moduesl
127does!). The trick is to first import the module again, then reload
128it. For instance, <CODE>import foo; reload(foo)</CODE>. Because the
129module object already exists internally, the import statement does not
130attempt to execute the module again -- it just places it in your
Jack Jansen08365421996-04-19 15:56:08 +0000131workspace. </BLOCKQUOTE>
Jack Jansen5f962c21996-04-10 14:52:59 +0000132
133<h2>Clickable python scripts</h2>
134
Jack Jansenbd9565a1996-04-15 12:25:44 +0000135If you create your script with the correct creator and type, creator
136<CODE>'Pyth'</CODE> and type <CODE>'TEXT'</CODE>, you can double-click
137your script and it will automatically invoke the interpreter. If you
138use BBEdit you can tell it about the Python file type by adding it to
139the "file types" sections of the preferences. Then, if you save a file
140for the first time you can tell BBEdit to save the file as a Python
141script through the "options" choice of the save dialog. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000142
Jack Jansenbd9565a1996-04-15 12:25:44 +0000143The <CODE>Scripts</CODE> folder contains a script
144<CODE>fixfiletypes</CODE> that will recursively traverse a folder and
145set the correct creator and type for all files ending in
146<CODE>.py</CODE>. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000147
Jack Jansen08365421996-04-19 15:56:08 +0000148<BLOCKQUOTE>
149Older releases of Python used the creator code
150<CODE>'PYTH'</CODE> in stead of <CODE>'Pyth'</CODE>. If you still have
151older Python sources on your system and named them with
152<CODE>'.py'</CODE> extension the <CODE>fixfiletypes</CODE> script will
153correct them.
154</BLOCKQUOTE>
155
Jack Jansen5f962c21996-04-10 14:52:59 +0000156<h2>Interaction with the user</h2>
157
Jack Jansenbd9565a1996-04-15 12:25:44 +0000158Normally, the interpreter will check for user input (mouse clicks,
159keyboard input) every once in a while, so it is possible to switch to
160other applications while a script runs. It is also possible to
161interrupt the interpreter with the standard command-period keypress,
162this will raise the <CODE>KeyboardInterrupt</CODE> exception. Scripts
163may, however, turn off this behaviour to facilitate their own event
164handling. Such scripts can only be killed with the
165command-option-escape shortcut.
Jack Jansen5f962c21996-04-10 14:52:59 +0000166
167<h2><A NAME="startup">startup options</A></h2>
168
Jack Jansenbd9565a1996-04-15 12:25:44 +0000169If the <EM>option</EM> key is depressed when Python starts executing
170the interpreter will bring up an options dialog thru which you can
171influence the way the interpreter behaves. Keep the option key
172depressed until the dialog comes up. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000173
174<img src="html.icons/options.gif"><p>
175
176The options modify the interpreters behaviour in the following way:
177<ul>
178<li> the interpreter goes to interactive mode (in stead of
179exiting) after a script has terminated normally,
180<li> for every module imported a line is printed telling you where the
181module was loaded from,
Jack Jansenbd9565a1996-04-15 12:25:44 +0000182<li> do not print the values of expressions executed as statements in
183an interactive python,
Jack Jansen5f962c21996-04-10 14:52:59 +0000184<li> do not buffer stdout and stderr,
185<li> print some debugging output during the parsing phase,
186<li> keep the output window open when a script terminates.
187</ul>
Jack Jansenbd9565a1996-04-15 12:25:44 +0000188In addition, you can enter a unix-style command line which is passed
189to the script in <CODE>sys.argv</CODE>. Sys.argv[0] is always the name
190of the script being executed, additional values can be passed
191here. Quoting works as expected. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000192
Jack Jansenbd9565a1996-04-15 12:25:44 +0000193The default options are also settable on a system-wide basis, see the
194section on <A HREF="#preferences">editing preferences</A>. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000195
196<h2>Module search path</h2>
197
Jack Jansenbd9565a1996-04-15 12:25:44 +0000198The module search path, <CODE>sys.path</CODE>, contains the folders
199python will search when you import a module. The path is settable on a
200system-wide basis (see the preferences section), and normally
201comprises the current folder (where the script lives), the
202<CODE>Lib</CODE> folder and some of its subfolders and possibly some
203more. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000204
205<h2>Working folder</h2>
206
Jack Jansenbd9565a1996-04-15 12:25:44 +0000207The unix concept of a <I>working directory</I> does not translate
208directly to a similar concept on the Macintosh. To facilitate easy
209porting and the use of relative pathnames in scripts the interpreter
210simulates a working directory. When a script is started the initial
211working directory is the folder where the script lives. In case of an
212interactive interpreter the working directory is the folder where the
213interpreter lives. <P>
214
215By the way: the "standard file" folder, the folder that is presented
216to the user initially for an <I>open</I> or <I>save</I> dialog, does
217<EM>not</EM> follow the Python working directory. Which folder is
218initially shown to the user is usually one of (a) the application
219folder, (b) the "Documents" folder or (c) the folder most recently
220used for such a dialog (in any Python program). This is standard MacOS
221behaviour, so don't blame Python for it. The exact behaviour is
222settable through a control panel since System 7.5.
Jack Jansen5f962c21996-04-10 14:52:59 +0000223
224<h2>Interactive startup file</h2>
225
Jack Jansenbd9565a1996-04-15 12:25:44 +0000226If the folder containing the interpreter contains a file named
227<CODE>PythonStartup</CODE> this file is executed when you start an
228interactive interpreter. In this file you could import modules you
229often use and other such things. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000230
231
232<h2>Compiled python scripts</h2>
233
Jack Jansenbd9565a1996-04-15 12:25:44 +0000234Once a python module has been imported the interpreter creates a
235compiled version which is stored in a file with the ".py" extension
236replaced by ".pyc". These compiled files, with creator
237<CODE>'Pyth'</CODE> and type <CODE>'PYC '</CODE> load faster when
238imported (because they do not have to be parsed). The <CODE>Lib</CODE>
239folder contains a script <CODE>compileall.py</CODE>, running this
240script will cause all modules along the python search path to be
241precompiled, which will speed up your programs. Compiled files are
242also double-clickable. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000243
244<h2>Python resources</h2>
245
Jack Jansenbd9565a1996-04-15 12:25:44 +0000246MacPython has the ability to collect a number of compiled modules
247together in the resource fork of a single file. This feature is useful
248if you distribute a python program and want to minimize clutter: you
249can put all the needed modules in a single file (which could even be
250the interpreter itself). <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000251
Jack Jansenbd9565a1996-04-15 12:25:44 +0000252If the module search path contains a filename as one of its entries
253(as opposed to a folder name, which is the normal case) this file will
254be searched for a resource with type <CODE>'PYC '</CODE> and a name
255matching the module being imported. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000256
Jack Jansenbd9565a1996-04-15 12:25:44 +0000257The <CODE>scripts</CODE> folder contains a script
258<CODE>PackLibDir</CODE> which will convert a number of modules (or
259possibly a complete subtree full of modules) into such a resource
260file.
Jack Jansen5f962c21996-04-10 14:52:59 +0000261
262<h2><A NAME="preferences">Setting interpreter preferences</A></h2>
263
Jack Jansenbd9565a1996-04-15 12:25:44 +0000264The python interpreter keeps a preferences file in the standard
265location in the system folder. In this preferences file it remembers
266the default module search path and the default settings for the
267runtime options. The preferences are settable via
268<CODE>EditPythonPrefs</CODE>. For PPC python this is a standalone
269program living in the main Python folder, for 68K python it is a
270script in the <CODE>Scripts</CODE> folder. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000271
Jack Jansenbd9565a1996-04-15 12:25:44 +0000272The interface to edit the preferences is rather clunky for the current
273release. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000274
275<img src="html.icons/preferences.gif"><p>
276
Jack Jansenbd9565a1996-04-15 12:25:44 +0000277In the editable text field at the top you enter the initial module
278search path, using newline as a separator. There are two special
279values you can use here: an initial substring <CODE>$(PYTHON)</CODE>
280will expand to the Python home folder and a value of
281<CODE>$(APPLICATION)</CODE> will expand to the the python application
282itself. Note that the text field may extend "beyond the bottom" even
283though it does not have a scroll bar. Using the arrow keys works,
284though.<p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000285
Jack Jansenbd9565a1996-04-15 12:25:44 +0000286The Python home folder $(PYTHON) is initially, when you execute the
287interpreter for the first time, set to the folder where the
288interpreter lives. You can change it here. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000289
Jack Jansenbd9565a1996-04-15 12:25:44 +0000290Finally, you can set the default startup options here, through a
291sub-dialog.
Jack Jansen5f962c21996-04-10 14:52:59 +0000292
293<h2>Applets</h2>
294
Jack Jansenbd9565a1996-04-15 12:25:44 +0000295An applet is a fullblown application written in Python, similar to an
296AppleScript applet (and completely different from a Java
297applet). Applets are currently only supported on PowerPC macintoshes,
298and are created using the <CODE>mkapplet</CODE> program. You create an
299applet by dropping the python source script onto mkapplet. The
300<CODE>Demo</CODE> folder contains an example of a more involved applet
301with its own resource file, etc. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000302
Jack Jansenbd9565a1996-04-15 12:25:44 +0000303Note that while an applet behaves as a fullblown Macintosh application
304it is not self-sufficient, so distributing it to a machine without an
305installed Python interpreter will not work: it needs the shared python
306execution engine <CODE>PythonCore</CODE>, and probably various modules
307from the Lib and PlugIns folders. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000308
309<h2>Customizing applets</h2>
310
Jack Jansenbd9565a1996-04-15 12:25:44 +0000311Applets can have their own settings for the startup options and module
312search path. Dropping an applet on the <CODE>EditPythonPrefs</CODE>
313application allows you to set these, in the same way as
314double-clicking EditPythonPrefs allows you to set the system-wide
315defaults. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000316
Jack Jansenbd9565a1996-04-15 12:25:44 +0000317Actually, not only applets but also the interpreter itself can have
318non-default settings for path and options. If you make a copy of the
319interpreter and drop this copy onto EditPythonPrefs you will have an
320interpreter that has a different set of default settings.
Jack Jansen5f962c21996-04-10 14:52:59 +0000321
322<h2>Where to go from here</h2>
323
Jack Jansenbd9565a1996-04-15 12:25:44 +0000324The previously mentioned <A
325HREF="http://www.python.org/doc/tut/tut.html">Python Tutorial</A> is
326an excellent place to start reading if you have never used Python
327before. Other documentation such as the library reference manual is
328indexed at the <A HREF="http://www.python.org/doc/">Python
329Documentation</A> page. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000330
Jack Jansenbd9565a1996-04-15 12:25:44 +0000331There are some <A HREF="index.html">annotated sample programs</A>
332available that show some mac-specific issues, like use of various
333toolboxes and creation of Python applets. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000334
Jack Jansenbd9565a1996-04-15 12:25:44 +0000335Finally, the <CODE>Demo</CODE> folder in the Macintosh distribution
336contains a number of other example programs. Most of these are only
337very lightly documented, but they may help you to understand some
338aspects of using Python. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000339
340The best way to contact fellow Macintosh Python programmers is to join
Jack Jansenbd9565a1996-04-15 12:25:44 +0000341the MacPython Special Interest Group mailing list. Send a message with
342"info" in the body to <A
343HREF="mailto:pythonmac-sig-request@python.org">pythonmac-sig-request@python.org</A>
344or view the <A
345HREF="http://www.python.org/sigs/pythonmac-sig/">Pythonmac SIG
346page</A> on the <A HREF="http://www.python.org">www.python.org</A> WWW
347server. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000348
349<h2>Troubleshooting</h2>
350
Jack Jansenbd9565a1996-04-15 12:25:44 +0000351Python is a rather safe language, and hence it should be difficult to
352crash the interpreter of the system with a Python script. There is an
353exception to this rule, though: the modules that interface to the
354system toolboxes (windowing, quickdraw, etc) do very little error
355checking and therefore a misbehaving program using these modules may
356indeed crash the system. Such programs are unfortunately rather
357difficult to debug, since the crash does not generate the standard
358Python stack trace, obviously, and since debugging print statements
359will often interfere with the operation of the program. There is
360little to do about this currently. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000361
Jack Jansenbd9565a1996-04-15 12:25:44 +0000362Probably the most common cause of problems with modules ported from
363other systems is the Mac end-of-line convention. Where unix uses
Jack Jansend9585c91996-05-07 15:28:20 +0000364linefeed, 0x0a, to separate lines the mac uses carriage return,
3650x0d. To complicate matters more a lot of mac programming editors like
Jack Jansenbd9565a1996-04-15 12:25:44 +0000366BBEdit and emacs will work happily with both conventions, so the file
367will appear to be correct in the editor but cause strange errors when
368imported. BBEdit has a popup menu which allows you to inspect (and
369set) the end-of-line convention used in a file. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000370
Jack Jansen024a3871996-07-18 16:07:05 +0000371<h2>Where to go from here</h2>
372
373The next section to check out is the <a href="index.html">annotated sample programs</a>.<p>
374
Jack Jansen5f962c21996-04-10 14:52:59 +0000375<HR>
376<A HREF="http://www.cwi.nl/~jack">Jack Jansen</A>,
Jack Jansen08365421996-04-19 15:56:08 +0000377<A HREF="mailto:jack@cwi.nl">jack@cwi.nl</A>, 19-Apr-1996.
Jack Jansen5f962c21996-04-10 14:52:59 +0000378
379</BODY>
380</HTML>