blob: 273ede2a16b3a43cfd023d1ee72c977df3a5ba55 [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 Jansen22fa6421996-09-05 15:15:59 +0000193<BLOCKQUOTE>
194<EM>Warning:</EM> redirecting standard input or standard output in the
195command-line dialog does not work. This is due to circumstances beyond my
196control, hence I cannot say when this will be fixed.
197</BLOCKQUOTE>
198
Jack Jansenbd9565a1996-04-15 12:25:44 +0000199The default options are also settable on a system-wide basis, see the
200section on <A HREF="#preferences">editing preferences</A>. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000201
202<h2>Module search path</h2>
203
Jack Jansenbd9565a1996-04-15 12:25:44 +0000204The module search path, <CODE>sys.path</CODE>, contains the folders
205python will search when you import a module. The path is settable on a
206system-wide basis (see the preferences section), and normally
207comprises the current folder (where the script lives), the
208<CODE>Lib</CODE> folder and some of its subfolders and possibly some
209more. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000210
211<h2>Working folder</h2>
212
Jack Jansenbd9565a1996-04-15 12:25:44 +0000213The unix concept of a <I>working directory</I> does not translate
214directly to a similar concept on the Macintosh. To facilitate easy
215porting and the use of relative pathnames in scripts the interpreter
216simulates a working directory. When a script is started the initial
217working directory is the folder where the script lives. In case of an
218interactive interpreter the working directory is the folder where the
219interpreter lives. <P>
220
221By the way: the "standard file" folder, the folder that is presented
222to the user initially for an <I>open</I> or <I>save</I> dialog, does
223<EM>not</EM> follow the Python working directory. Which folder is
224initially shown to the user is usually one of (a) the application
225folder, (b) the "Documents" folder or (c) the folder most recently
226used for such a dialog (in any Python program). This is standard MacOS
227behaviour, so don't blame Python for it. The exact behaviour is
228settable through a control panel since System 7.5.
Jack Jansen5f962c21996-04-10 14:52:59 +0000229
230<h2>Interactive startup file</h2>
231
Jack Jansenbd9565a1996-04-15 12:25:44 +0000232If the folder containing the interpreter contains a file named
233<CODE>PythonStartup</CODE> this file is executed when you start an
234interactive interpreter. In this file you could import modules you
235often use and other such things. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000236
237
238<h2>Compiled python scripts</h2>
239
Jack Jansenbd9565a1996-04-15 12:25:44 +0000240Once a python module has been imported the interpreter creates a
241compiled version which is stored in a file with the ".py" extension
242replaced by ".pyc". These compiled files, with creator
243<CODE>'Pyth'</CODE> and type <CODE>'PYC '</CODE> load faster when
244imported (because they do not have to be parsed). The <CODE>Lib</CODE>
245folder contains a script <CODE>compileall.py</CODE>, running this
246script will cause all modules along the python search path to be
247precompiled, which will speed up your programs. Compiled files are
248also double-clickable. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000249
250<h2>Python resources</h2>
251
Jack Jansenbd9565a1996-04-15 12:25:44 +0000252MacPython has the ability to collect a number of compiled modules
253together in the resource fork of a single file. This feature is useful
254if you distribute a python program and want to minimize clutter: you
255can put all the needed modules in a single file (which could even be
256the interpreter itself). <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000257
Jack Jansenbd9565a1996-04-15 12:25:44 +0000258If the module search path contains a filename as one of its entries
259(as opposed to a folder name, which is the normal case) this file will
260be searched for a resource with type <CODE>'PYC '</CODE> and a name
261matching the module being imported. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000262
Jack Jansenbd9565a1996-04-15 12:25:44 +0000263The <CODE>scripts</CODE> folder contains a script
264<CODE>PackLibDir</CODE> which will convert a number of modules (or
265possibly a complete subtree full of modules) into such a resource
266file.
Jack Jansen5f962c21996-04-10 14:52:59 +0000267
268<h2><A NAME="preferences">Setting interpreter preferences</A></h2>
269
Jack Jansenbd9565a1996-04-15 12:25:44 +0000270The python interpreter keeps a preferences file in the standard
271location in the system folder. In this preferences file it remembers
272the default module search path and the default settings for the
273runtime options. The preferences are settable via
274<CODE>EditPythonPrefs</CODE>. For PPC python this is a standalone
275program living in the main Python folder, for 68K python it is a
276script in the <CODE>Scripts</CODE> folder. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000277
Jack Jansenbd9565a1996-04-15 12:25:44 +0000278The interface to edit the preferences is rather clunky for the current
279release. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000280
281<img src="html.icons/preferences.gif"><p>
282
Jack Jansenbd9565a1996-04-15 12:25:44 +0000283In the editable text field at the top you enter the initial module
284search path, using newline as a separator. There are two special
285values you can use here: an initial substring <CODE>$(PYTHON)</CODE>
286will expand to the Python home folder and a value of
287<CODE>$(APPLICATION)</CODE> will expand to the the python application
288itself. Note that the text field may extend "beyond the bottom" even
289though it does not have a scroll bar. Using the arrow keys works,
290though.<p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000291
Jack Jansenbd9565a1996-04-15 12:25:44 +0000292The Python home folder $(PYTHON) is initially, when you execute the
293interpreter for the first time, set to the folder where the
294interpreter lives. You can change it here. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000295
Jack Jansenbd9565a1996-04-15 12:25:44 +0000296Finally, you can set the default startup options here, through a
297sub-dialog.
Jack Jansen5f962c21996-04-10 14:52:59 +0000298
299<h2>Applets</h2>
300
Jack Jansenbd9565a1996-04-15 12:25:44 +0000301An applet is a fullblown application written in Python, similar to an
302AppleScript applet (and completely different from a Java
303applet). Applets are currently only supported on PowerPC macintoshes,
304and are created using the <CODE>mkapplet</CODE> program. You create an
305applet by dropping the python source script onto mkapplet. The
306<CODE>Demo</CODE> folder contains an example of a more involved applet
307with its own resource file, etc. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000308
Jack Jansenbd9565a1996-04-15 12:25:44 +0000309Note that while an applet behaves as a fullblown Macintosh application
310it is not self-sufficient, so distributing it to a machine without an
311installed Python interpreter will not work: it needs the shared python
312execution engine <CODE>PythonCore</CODE>, and probably various modules
313from the Lib and PlugIns folders. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000314
315<h2>Customizing applets</h2>
316
Jack Jansenbd9565a1996-04-15 12:25:44 +0000317Applets can have their own settings for the startup options and module
318search path. Dropping an applet on the <CODE>EditPythonPrefs</CODE>
319application allows you to set these, in the same way as
320double-clicking EditPythonPrefs allows you to set the system-wide
321defaults. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000322
Jack Jansenbd9565a1996-04-15 12:25:44 +0000323Actually, not only applets but also the interpreter itself can have
324non-default settings for path and options. If you make a copy of the
325interpreter and drop this copy onto EditPythonPrefs you will have an
326interpreter that has a different set of default settings.
Jack Jansen5f962c21996-04-10 14:52:59 +0000327
328<h2>Where to go from here</h2>
329
Jack Jansenbd9565a1996-04-15 12:25:44 +0000330The previously mentioned <A
331HREF="http://www.python.org/doc/tut/tut.html">Python Tutorial</A> is
332an excellent place to start reading if you have never used Python
333before. Other documentation such as the library reference manual is
334indexed at the <A HREF="http://www.python.org/doc/">Python
335Documentation</A> page. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000336
Jack Jansenbd9565a1996-04-15 12:25:44 +0000337There are some <A HREF="index.html">annotated sample programs</A>
338available that show some mac-specific issues, like use of various
339toolboxes and creation of Python applets. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000340
Jack Jansenbd9565a1996-04-15 12:25:44 +0000341Finally, the <CODE>Demo</CODE> folder in the Macintosh distribution
342contains a number of other example programs. Most of these are only
343very lightly documented, but they may help you to understand some
344aspects of using Python. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000345
346The best way to contact fellow Macintosh Python programmers is to join
Jack Jansenbd9565a1996-04-15 12:25:44 +0000347the MacPython Special Interest Group mailing list. Send a message with
348"info" in the body to <A
349HREF="mailto:pythonmac-sig-request@python.org">pythonmac-sig-request@python.org</A>
350or view the <A
351HREF="http://www.python.org/sigs/pythonmac-sig/">Pythonmac SIG
352page</A> on the <A HREF="http://www.python.org">www.python.org</A> WWW
353server. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000354
355<h2>Troubleshooting</h2>
356
Jack Jansenbd9565a1996-04-15 12:25:44 +0000357Python is a rather safe language, and hence it should be difficult to
358crash the interpreter of the system with a Python script. There is an
359exception to this rule, though: the modules that interface to the
360system toolboxes (windowing, quickdraw, etc) do very little error
361checking and therefore a misbehaving program using these modules may
362indeed crash the system. Such programs are unfortunately rather
363difficult to debug, since the crash does not generate the standard
364Python stack trace, obviously, and since debugging print statements
365will often interfere with the operation of the program. There is
366little to do about this currently. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000367
Jack Jansenbd9565a1996-04-15 12:25:44 +0000368Probably the most common cause of problems with modules ported from
369other systems is the Mac end-of-line convention. Where unix uses
Jack Jansend9585c91996-05-07 15:28:20 +0000370linefeed, 0x0a, to separate lines the mac uses carriage return,
3710x0d. To complicate matters more a lot of mac programming editors like
Jack Jansenbd9565a1996-04-15 12:25:44 +0000372BBEdit and emacs will work happily with both conventions, so the file
373will appear to be correct in the editor but cause strange errors when
374imported. BBEdit has a popup menu which allows you to inspect (and
375set) the end-of-line convention used in a file. <p>
Jack Jansen5f962c21996-04-10 14:52:59 +0000376
Jack Jansen024a3871996-07-18 16:07:05 +0000377<h2>Where to go from here</h2>
378
379The next section to check out is the <a href="index.html">annotated sample programs</a>.<p>
380
Jack Jansen5f962c21996-04-10 14:52:59 +0000381<HR>
382<A HREF="http://www.cwi.nl/~jack">Jack Jansen</A>,
Jack Jansen22fa6421996-09-05 15:15:59 +0000383<A HREF="mailto:jack@cwi.nl">jack@cwi.nl</A>, 05-Sep-1996.
Jack Jansen5f962c21996-04-10 14:52:59 +0000384
385</BODY>
386</HTML>