blob: 8fbbe682b474b2b2b39203cfbd78c2bc06615630 [file] [log] [blame]
Jack Jansen7aeba452003-02-12 09:58:33 +00001\chapter{Using Python on a Mac OS 9 Macintosh \label{using}}
Fred Drake45aa5732000-10-14 05:09:42 +00002\sectionauthor{Bob Savage}{bobsavage@mac.com}
3
Jack Jansen7aeba452003-02-12 09:58:33 +00004Using Python on a Mac OS 9 Macintosh can seem like something completely
Fred Drake45aa5732000-10-14 05:09:42 +00005different than using it on a \UNIX-like or Windows system. Most of the
6Python documentation, both the ``official'' documentation and
7published books, describe only how Python is used on these systems,
Jack Jansen7aeba452003-02-12 09:58:33 +00008causing confusion for the new user of MacPython-OS9. This chapter gives a
Fred Drake45aa5732000-10-14 05:09:42 +00009brief introduction to the specifics of using Python on a Macintosh.
10
Jack Jansen7aeba452003-02-12 09:58:33 +000011Note that this chapter is mainly relevant to Mac OS 9: MacPython-OSX
12is a superset of a normal unix Python. While MacPython-OS9 runs fine
13on Mac OS X it is a better choice to use MacPython-OSX there.
Fred Drake45aa5732000-10-14 05:09:42 +000014
Jack Jansen7aeba452003-02-12 09:58:33 +000015The section on the IDE (see Section \ref{IDE}) is relevant to MacPython-OSX
16too.
17
18\section{Getting and Installing MacPython-OS9 \label{getting}}
Fred Drake45aa5732000-10-14 05:09:42 +000019
20The most recent release version as well as possible newer experimental
21versions are best found at the MacPython page maintained by Jack
Fred Drake5e31c3f2002-10-18 18:16:19 +000022Jansen: \url{http://www.cwi.nl/\textasciitilde jack/macpython.html}.
Fred Drake45aa5732000-10-14 05:09:42 +000023
24
25Please refer to the \file{README} included with your distribution for
26the most up-to-date instructions.
27
28
29\section{Entering the interactive Interpreter
30 \label{interpreter}}
31
32The interactive interpreter that you will see used in Python
33documentation is started by double-clicking the
34\program{PythonInterpreter} icon, which looks like a 16-ton weight
35falling. You should see the version information and the
Fred Drake24413662001-04-13 17:32:49 +000036\samp{>\code{>}>~} prompt. Use it exactly as described in the
37standard documentation.
Fred Drake45aa5732000-10-14 05:09:42 +000038
39
40\section{How to run a Python script}
41
42There are several ways to run an existing Python script; two common
43ways to run a Python script are ``drag and drop'' and ``double
44clicking''. Other ways include running it from within the IDE (see
45Section \ref{IDE}), or launching via AppleScript.
46
47
48\subsection{Drag and drop}
49
50One of the easiest ways to launch a Python script is via ``Drag and
51Drop''. This is just like launching a text file in the Finder by
52``dragging'' it over your word processor's icon and ``dropping'' it
53there. Make sure that you use an icon referring to the
54\program{PythonInterpreter}, not the \program{IDE} or \program{Idle}
55icons which have different behaviour which is described below.
56
57Some things that might have gone wrong:
58
59\begin{itemize}
60\item
61A window flashes after dropping the script onto the
62\program{PythonInterpreter}, but then disappears. Most likely this is a
63configuration issue; your \program{PythonInterpreter} is setup to exit
64immediately upon completion, but your script assumes that if it prints
65something that text will stick around for a while. To fix this, see
Fred Drake8e850ee2000-10-14 05:24:20 +000066section \ref{defaults}.
Fred Drake45aa5732000-10-14 05:09:42 +000067
68\item
Fred Drake45aa5732000-10-14 05:09:42 +000069When you waved the script icon over the \program{PythonInterpreter},
70the \program{PythonInterpreter} icon did not hilight. Most likely the
71Creator code and document type is unset (or set incorrectly) -- this
72often happens when a file originates on a non-Mac computer. See
Fred Drake8e850ee2000-10-14 05:24:20 +000073section \ref{creator-code} for more details.
Fred Drake45aa5732000-10-14 05:09:42 +000074\end{itemize}
75
76
77\subsection{Set Creator and Double Click \label{creator-code}}
78
79If the script that you want to launch has the appropriate Creator Code
80and File Type you can simply double-click on the script to launch it.
81To be ``double-clickable'' a file needs to be of type \samp{TEXT},
82with a creator code of \samp{Pyth}.
83
84Setting the creator code and filetype can be done with the IDE (see
85sections \ref{IDEwrite} and \ref{IDEapplet}), with an editor with a
86Python mode (\program{BBEdit}) -- see section
87\ref{scripting-with-BBedit}, or with assorted other Mac utilities, but
88a script (\file{fixfiletypes.py}) has been included in the MacPython
89distribution, making it possible to set the proper Type and Creator
90Codes with Python.
91
92The \file{fixfiletypes.py} script will change the file type and
93creator codes for the indicated directory. To use
94\file{fixfiletypes.py}:
95
96\begin{enumerate}
97\item
98Locate it in the \file{scripts} folder of the \file{Mac} folder of the
99MacPython distribution.
100
101\item
102Put all of the scripts that you want to fix in a folder with nothing
103else in it.
104
105\item
106Double-click on the \file{fixfiletypes.py} icon.
107
108\item
109Navigate into the folder of files you want to fix, and press the
110``Select current folder'' button.
111\end{enumerate}
112
113
114\section{Simulating command line arguments
115 \label{argv}}
116
Jack Jansen7aeba452003-02-12 09:58:33 +0000117There are two ways to simulate command-line arguments with MacPython-OS9.
Fred Drake45aa5732000-10-14 05:09:42 +0000118
119\begin{enumerate}
120\item via Interpreter options
121\begin{itemize} % nestable? I hope so!
122 \item Hold the option-key down when launching your script. This will
123 bring up a dialog box of Python Interpreter options.
124 \item Click ``Set \UNIX-style command line..'' button.
125 \item Type the arguments into the ``Argument'' field.
126 \item Click ``OK''
127 \item Click ``Run''.
128\end{itemize} % end
129
130\item via drag and drop
131If you save the script as an applet (see Section \ref{IDEapplet}), you
132can also simulate some command-line arguments via
133``Drag-and-Drop''. In this case, the names of the files that were
134dropped onto the applet will be appended to \code{sys.argv}, so that
135it will appear to the script as though they had been typed on a
136command line. As on \UNIX\ systems, the first item in \code{sys.srgv} is
137the path to the applet, and the rest are the files dropped on the
138applet.
139\end{enumerate}
140
141
142\section{Creating a Python script}
143
144Since Python scripts are simply text files, they can be created in any
145way that text files can be created, but some special tools also exist
146with extra features.
147
148
149\subsection{In an editor}
150
151You can create a text file with any word processing program such as
152\program{MSWord} or \program{AppleWorks} but you need to make sure
153that the file is saved as ``\ASCII'' or ``plain text''.
154
155
156\subsubsection{Editors with Python modes}
157
158Several text editors have additional features that add functionality
159when you are creating a Python script. These can include coloring
160Python keywords to make your code easier to read, module browsing, or
161a built-in debugger. These include \program{Alpha}, \program{Pepper},
162and \program{BBedit}, and the MacPython IDE (Section \ref{IDE}).
163
164%\subsubsection{Alpha}
165% **NEED INFO HERE**
166
167\subsubsection{BBedit \label{scripting-with-BBedit}}
168
169If you use \program{BBEdit} to create your scripts you will want to tell it about the Python creator code so that
170you can simply double click on the saved file to launch it.
171\begin{itemize}
172 \item Launch \program{BBEdit}.
173 \item Select ``Preferences'' from the ``Edit'' menu.
174 \item Select ``File Types'' from the scrolling list.
175 \item click on the ``Add...'' button and navigate to
176 \program{PythonInterpreter} in the main directory of the
177 MacPython distribution; click ``open''.
178 \item Click on the ``Save'' button in the Preferences panel.
179\end{itemize}
180% Are there additional BBedit Python-specific features? I'm not aware of any.
181
182%\subsubsection{IDE}
183%You can use the \program{Python IDE} supplied in the MacPython Distribution to create longer Python scripts
184%-- see Section \ref{IDEwrite} for details.
185
186%\subsubsection{IDLE}
187%Idle is an IDE for Python that was written in Python, using TKInter. You should be able to use it on a Mac by following
188%the standard documentation, but see Section \ref{TKInter} for guidance on using TKInter with MacPython.
189
190%\subsubsection{Pepper}
191% **NEED INFO HERE**
192
193
194\section{The IDE\label{IDE}}
195
196The \program{Python IDE} (Integrated Development Environment) is a
197separate application that acts as a text editor for your Python code,
198a class browser, a graphical debugger, and more.
199
200
201\subsection{Using the ``Python Interactive'' window}
202
203Use this window like you would the \program{PythonInterpreter}, except
204that you cannot use the ``Drag and drop'' method above. Instead,
205dropping a script onto the \program{Python IDE} icon will open the
Fred Drake38080452001-11-01 19:48:01 +0000206file in a separate script window (which you can then execute manually
Fred Drake45aa5732000-10-14 05:09:42 +0000207-- see section \ref{IDEexecution}).
208
209
210\subsection{Writing a Python Script \label{IDEwrite}}
211
212In addition to using the \program{Python IDE} interactively, you can
213also type out a complete Python program, saving it incrementally, and
214execute it or smaller selections of it.
215
216You can create a new script, open a previously saved script, and save
217your currently open script by selecting the appropriate item in the
218``File'' menu. Dropping a Python script onto the
219\program{Python IDE} will open it for editting.
220
221If you try to open a script with the \program{Python IDE} but either
222can't locate it from the ``Open'' dialog box, or you get an error
223message like ``Can't open file of type ...'' see section
Fred Drake8e850ee2000-10-14 05:24:20 +0000224\ref{creator-code}.
Fred Drake45aa5732000-10-14 05:09:42 +0000225
226When the \program{Python IDE} saves a script, it uses the creator code
227settings which are available by clicking on the small black triangle
228on the top right of the document window, and selecting ``save
229options''. The default is to save the file with the \program{Python
230IDE} as the creator, this means that you can open the file for editing
231by simply double-clicking on its icon. You might want to change this
232behaviour so that it will be opened by the
233\program{PythonInterpreter}, and run. To do this simply choose
234``Python Interpreter'' from the ``save options''. Note that these
235options are associated with the \emph{file} not the application.
236
237
238\subsection{Executing a script from within the IDE
239 \label{IDEexecution}}
240
241You can run the script in the frontmost window of the \program{Python
242IDE} by hitting the run all button. You should be aware, however that
243if you use the Python convention \samp{if __name__ == "__main__":} the
244script will \emph{not} be ``__main__'' by default. To get that
245behaviour you must select the ``Run as __main__'' option from the
246small black triangle on the top right of the document window. Note
247that this option is associated with the \emph{file} not the
248application. It \emph{will} stay active after a save, however; to shut
249this feature off simply select it again.
250
251
252\subsection{``Save as'' versus ``Save as Applet''
253 \label{IDEapplet}}
254
255When you are done writing your Python script you have the option of
256saving it as an ``applet'' (by selecting ``Save as applet'' from the
257``File'' menu). This has a significant advantage in that you can drop
258files or folders onto it, to pass them to the applet the way
259command-line users would type them onto the command-line to pass them
260as arguments to the script. However, you should make sure to save the
Fred Drake38080452001-11-01 19:48:01 +0000261applet as a separate file, do not overwrite the script you are
Fred Drake45aa5732000-10-14 05:09:42 +0000262writing, because you will not be able to edit it again.
263
264Accessing the items passed to the applet via ``drag-and-drop'' is done
265using the standard \member{sys.argv} mechanism. See the general
266documentation for more
267% need to link to the appropriate place in non-Mac docs
268
269Note that saving a script as an applet will not make it runnable on a
270system without a Python installation.
271
272%\subsection{Debugger}
273% **NEED INFO HERE**
274
275%\subsection{Module Browser}
276% **NEED INFO HERE**
277
278%\subsection{Profiler}
279% **NEED INFO HERE**
280% end IDE
281
282%\subsection{The ``Scripts'' menu}
283% **NEED INFO HERE**
284
285\section{Configuration \label{configuration}}
286
287The MacPython distribution comes with \program{EditPythonPrefs}, an
288applet which will help you to customize the MacPython environment for
289your working habits.
290
291\subsection{EditPythonPrefs\label{EditPythonPrefs}}
292
293\program{EditPythonPrefs} gives you the capability to configure Python
294to behave the way you want it to. There are two ways to use
295\program{EditPythonPrefs}, you can use it to set the preferences in
296general, or you can drop a particular Python engine onto it to
297customize only that version. The latter can be handy if, for example,
298you want to have a second copy of the \program{PythonInterpreter} that
299keeps the output window open on a normal exit even though you prefer
300to normally not work that way.
301
302To change the default preferences, simply double-click on
303\program{EditPythonPrefs}. To change the preferences only for one copy
304of the Interpreter, drop the icon for that copy onto
305\program{EditPythonPrefs}. You can also use \program{EditPythonPrefs}
306in this fashion to set the preferences of the \program{Python IDE} and
Fred Drake8e850ee2000-10-14 05:24:20 +0000307any applets you create -- see section %s \ref{BuildApplet} and
Fred Drake45aa5732000-10-14 05:09:42 +0000308\ref{IDEapplet}.
309
310\subsection{Adding modules to the Module Search Path
311 \label{search-path}}
312
313When executing an \keyword{import} statement, Python looks for modules
314in places defined by the \member{sys.path} To edit the
315\member{sys.path} on a Mac, launch \program{EditPythonPrefs}, and
316enter them into the largish field at the top (one per line).
317
318Since MacPython defines a main Python directory, the easiest thing is
319to add folders to search within the main Python directory. To add a
320folder of scripts that you created called ``My Folder'' located in the
321main Python Folder, enter \samp{\$(PYTHON):My Folder} onto a new line.
322
323To add the Desktop under OS 9 or below, add
324\samp{StartupDriveName:Desktop Folder} on a new line.
325
326\subsection{Default startup options \label{defaults}}
327
328% I'm assuming that there exists some other documentation on the
329% rest of the options so I only go over a couple here.
330
331The ``Default startup options...'' button in the
332\program{EditPythonPrefs} dialog box gives you many options including
333the ability to keep the ``Output'' window open after the script
334terminates, and the ability to enter interactive mode after the
335termination of the run script. The latter can be very helpful if you
336want to examine the objects that were created during your script.
337
338%\section{Nifty Tools}
339%There are many other tools included with the MacPython
340%distribution. In addition to those discussed here, make
341%sure to check the \file{Mac} directory.
342
343%\subsection{BuildApplet \label{BuildApplet}}
344% **NEED INFO HERE**
345
346%\subsection{BuildApplication}
347% **NEED INFO HERE**
348
349%\section{TKInter on the Mac \label{TKInter}}
350
351%TKinter is installed by default with the MacPython distribution, but
352%you may need to add the \file{lib-tk} folder to the Python Path (see
353%section \ref{search-path}). Also, it is important that you do not
354%try to launch Tk from within the \program{Python IDE} because the two
355%event loops will collide -- always run a script which uses Tkinter
356%with the \program{PythonInterpreter} instead -- see section
357%\ref{interpreter}.
358
359%\section{CGI on the Mac with Python \label{CGI}}
360%**NEED INFO HERE**