blob: 491d04c77f29d176d64032ddb7f6bacbfc841e91 [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 Jansend5e0a5a2003-04-09 15:12:38 +00004Using Python on a Macintosh, especially on Mac OS 9 (MacPython-OSX
Fred Drakef1c98692003-07-22 01:09:22 +00005includes a complete \UNIX{} Python) can seem like something completely
Fred Drake45aa5732000-10-14 05:09:42 +00006different than using it on a \UNIX-like or Windows system. Most of the
Jack Jansend5e0a5a2003-04-09 15:12:38 +00007Python documentation, both the ``official'' documentation and published
8books, describe only how Python is used on these systems, causing
9confusion for the new user of MacPython-OS9. This chapter gives a brief
10introduction to the specifics of using Python on a Macintosh.
Fred Drake45aa5732000-10-14 05:09:42 +000011
12
Jack Jansen7aeba452003-02-12 09:58:33 +000013The section on the IDE (see Section \ref{IDE}) is relevant to MacPython-OSX
14too.
15
Jack Jansend5e0a5a2003-04-09 15:12:38 +000016\section{Getting and Installing MacPython-OSX \label{getting-OSX}}
17
18As of Python 2.3a2 the only sure way of getting MacPython-OSX on your machine
19is getting a source distribution and building what is called a "framework Python".
20The details are in the file \file{Mac/OSX/README}.
21
22As binary installers become available the details will be posted to
23\url{http://www.cwi.nl/\textasciitilde jack/macpython.html}.
24
25
26What you get after installing is a number of things:
27
28\begin{itemize}
29 \item A \file{MacPython-2.3} folder in your \file{Applications}
30 folder. In here you find the PythonIDE Integrated Development Environment;
Fred Drakef1c98692003-07-22 01:09:22 +000031 PythonLauncher, which handles double-clicking Python scripts from
32 the Finder; and the Package Manager.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000033
Fred Drakef1c98692003-07-22 01:09:22 +000034 \item A fairly standard \UNIX{} commandline Python interpreter in
35 \file{/usr/local/bin/python}, but without the usual
36 \file{/usr/local/lib/python}.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000037
38 \item A framework \file{/Library/Frameworks/Python.framework}, where
39 all the action really is, but which you usually do not have to be aware of.
40\end{itemize}
41
42To uninstall MacPython you can simply remove these three things.
43
44PythonIDE contains an Apple Help Viewer book called "MacPython Help"
45which you can access through its help menu. If you are completely new to
46Python you should start reading the IDE introduction in that document.
47
Fred Drakef1c98692003-07-22 01:09:22 +000048If you are familiar with Python on other \UNIX{} platforms you should
49read the section on running Python scripts from the \UNIX{} shell.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000050
51\subsection{How to run a Python script}
52
53Your best way to get started with Python on Mac OS X is through the PythonIDE
54integrated development environment, see section \ref{IDE} and use the Help
55menu when the IDE is running.
56
57If you want to run Python scripts from the Terminal window command line
58or from the Finder you first need an editor to create your script.
Fred Drakef1c98692003-07-22 01:09:22 +000059Mac OS X comes with a number of standard \UNIX{} command line editors,
Jack Jansend5e0a5a2003-04-09 15:12:38 +000060\program{vi} and \program{emacs} among them. If you want a more Mac-like
61editor \program{BBEdit} or \program{TextWrangler} from Bare Bones Software
Fred Drakef1c98692003-07-22 01:09:22 +000062(see \url{http://www.barebones.com/products/bbedit/index.shtml}) are
63good choices. Their freeware \program{BBEdit Lite} is officially
64discontinued but still available. \program{AppleWorks} or any other
65word processor that can save files in ASCII is also a possibility, but
66\program{TextEdit} is not: it saves in \file{.rtf} format only.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000067
68To run your script from the Terminal window you must make sure that
69\file{/usr/local/bin} is in your shell search path before \file{/usr/bin},
70where the Apple-supplied Python lives (which is version 2.2, as of Mac OS X
Jack Jansenbae5c962003-04-11 15:35:28 +00007110.2.4).
Jack Jansend5e0a5a2003-04-09 15:12:38 +000072
73To run your script from the Finder you have two options:
74\begin{itemize}
75 \item Drag it to \program{PythonLauncher}
76 \item Select \program{PythonLauncher} as the default application
77 to open your script (or any .py script) through the finder Info window
78 and double-click it.
79\end{itemize}
80
81PythonLauncher has various preferences to control how your script is launched.
82Option-dragging allows you to change these for one invocation, or use its
83Preferences menu to change things globally.
84
Jack Jansenbae5c962003-04-11 15:35:28 +000085\subsection{Running scripts with a GUI \label{osx-gui-scripts}}
86
87There is one Mac OS X quirk that you need to be aware of: programs
88that talk to the Aqua window manager (in other words, anything that has a GUI)
Neal Norwitz1ecbd672003-12-14 15:02:54 +000089need to be run in a special way. Use \program{pythonw} instead of \program{python}
Jack Jansenbae5c962003-04-11 15:35:28 +000090to start such scripts.
91
Jack Jansend5e0a5a2003-04-09 15:12:38 +000092\subsection{configuration}
93
Fred Drakef1c98692003-07-22 01:09:22 +000094MacPython honours all standard \UNIX{} environment variables such as
95\envvar{PYTHONPATH}, but setting these variables for programs started
96from the Finder is non-standard
Jack Jansend5e0a5a2003-04-09 15:12:38 +000097as the Finder does not read your \file{.profile} or \file{.cshrc} at startup.
98You need to create a file \file{\textasciitilde /.MacOSX/environment.plist}.
99See Apple's Technical Document QA1067 for details.
100
101Installing additional Python packages is most easily done through the
102Package Manager, see the MacPython Help Book for details.
103
Jack Jansen7aeba452003-02-12 09:58:33 +0000104\section{Getting and Installing MacPython-OS9 \label{getting}}
Fred Drake45aa5732000-10-14 05:09:42 +0000105
106The most recent release version as well as possible newer experimental
107versions are best found at the MacPython page maintained by Jack
Fred Drakef1c98692003-07-22 01:09:22 +0000108Jansen: \url{http://homepages.cwi.nl/\textasciitilde jack/macpython.html}.
Fred Drake45aa5732000-10-14 05:09:42 +0000109
Fred Drake45aa5732000-10-14 05:09:42 +0000110Please refer to the \file{README} included with your distribution for
111the most up-to-date instructions.
112
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000113Note that MacPython-OS9 runs fine on Mac OS X, and it runs in native
114mode, not in the Classic environment. Unless you have specific
115requirements for a CFM-based Python there is no reason not to
116use MacPython-OSX, though.
Fred Drake45aa5732000-10-14 05:09:42 +0000117
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000118
119\subsection{Entering the interactive Interpreter
Fred Drake45aa5732000-10-14 05:09:42 +0000120 \label{interpreter}}
121
122The interactive interpreter that you will see used in Python
123documentation is started by double-clicking the
124\program{PythonInterpreter} icon, which looks like a 16-ton weight
125falling. You should see the version information and the
Fred Drake24413662001-04-13 17:32:49 +0000126\samp{>\code{>}>~} prompt. Use it exactly as described in the
127standard documentation.
Fred Drake45aa5732000-10-14 05:09:42 +0000128
129
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000130\subsection{How to run a Python script}
Fred Drake45aa5732000-10-14 05:09:42 +0000131
132There are several ways to run an existing Python script; two common
133ways to run a Python script are ``drag and drop'' and ``double
134clicking''. Other ways include running it from within the IDE (see
135Section \ref{IDE}), or launching via AppleScript.
136
137
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000138\subsubsection{Drag and drop}
Fred Drake45aa5732000-10-14 05:09:42 +0000139
140One of the easiest ways to launch a Python script is via ``Drag and
141Drop''. This is just like launching a text file in the Finder by
142``dragging'' it over your word processor's icon and ``dropping'' it
143there. Make sure that you use an icon referring to the
144\program{PythonInterpreter}, not the \program{IDE} or \program{Idle}
145icons which have different behaviour which is described below.
146
147Some things that might have gone wrong:
148
149\begin{itemize}
150\item
151A window flashes after dropping the script onto the
152\program{PythonInterpreter}, but then disappears. Most likely this is a
153configuration issue; your \program{PythonInterpreter} is setup to exit
154immediately upon completion, but your script assumes that if it prints
155something that text will stick around for a while. To fix this, see
Fred Drake8e850ee2000-10-14 05:24:20 +0000156section \ref{defaults}.
Fred Drake45aa5732000-10-14 05:09:42 +0000157
158\item
Fred Drake45aa5732000-10-14 05:09:42 +0000159When you waved the script icon over the \program{PythonInterpreter},
160the \program{PythonInterpreter} icon did not hilight. Most likely the
161Creator code and document type is unset (or set incorrectly) -- this
162often happens when a file originates on a non-Mac computer. See
Fred Drake8e850ee2000-10-14 05:24:20 +0000163section \ref{creator-code} for more details.
Fred Drake45aa5732000-10-14 05:09:42 +0000164\end{itemize}
165
166
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000167\subsubsection{Set Creator and Double Click \label{creator-code}}
Fred Drake45aa5732000-10-14 05:09:42 +0000168
169If the script that you want to launch has the appropriate Creator Code
170and File Type you can simply double-click on the script to launch it.
171To be ``double-clickable'' a file needs to be of type \samp{TEXT},
172with a creator code of \samp{Pyth}.
173
174Setting the creator code and filetype can be done with the IDE (see
175sections \ref{IDEwrite} and \ref{IDEapplet}), with an editor with a
176Python mode (\program{BBEdit}) -- see section
177\ref{scripting-with-BBedit}, or with assorted other Mac utilities, but
178a script (\file{fixfiletypes.py}) has been included in the MacPython
179distribution, making it possible to set the proper Type and Creator
180Codes with Python.
181
182The \file{fixfiletypes.py} script will change the file type and
183creator codes for the indicated directory. To use
184\file{fixfiletypes.py}:
185
186\begin{enumerate}
187\item
188Locate it in the \file{scripts} folder of the \file{Mac} folder of the
189MacPython distribution.
190
191\item
192Put all of the scripts that you want to fix in a folder with nothing
193else in it.
194
195\item
196Double-click on the \file{fixfiletypes.py} icon.
197
198\item
199Navigate into the folder of files you want to fix, and press the
200``Select current folder'' button.
201\end{enumerate}
202
203
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000204\subsection{Simulating command line arguments
Fred Drake45aa5732000-10-14 05:09:42 +0000205 \label{argv}}
206
Jack Jansen7aeba452003-02-12 09:58:33 +0000207There are two ways to simulate command-line arguments with MacPython-OS9.
Fred Drake45aa5732000-10-14 05:09:42 +0000208
209\begin{enumerate}
210\item via Interpreter options
211\begin{itemize} % nestable? I hope so!
212 \item Hold the option-key down when launching your script. This will
213 bring up a dialog box of Python Interpreter options.
214 \item Click ``Set \UNIX-style command line..'' button.
215 \item Type the arguments into the ``Argument'' field.
216 \item Click ``OK''
217 \item Click ``Run''.
218\end{itemize} % end
219
220\item via drag and drop
221If you save the script as an applet (see Section \ref{IDEapplet}), you
222can also simulate some command-line arguments via
223``Drag-and-Drop''. In this case, the names of the files that were
224dropped onto the applet will be appended to \code{sys.argv}, so that
225it will appear to the script as though they had been typed on a
226command line. As on \UNIX\ systems, the first item in \code{sys.srgv} is
227the path to the applet, and the rest are the files dropped on the
228applet.
229\end{enumerate}
230
231
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000232\subsection{Creating a Python script}
Fred Drake45aa5732000-10-14 05:09:42 +0000233
234Since Python scripts are simply text files, they can be created in any
235way that text files can be created, but some special tools also exist
236with extra features.
237
238
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000239\subsubsection{In an editor}
Fred Drake45aa5732000-10-14 05:09:42 +0000240
241You can create a text file with any word processing program such as
242\program{MSWord} or \program{AppleWorks} but you need to make sure
243that the file is saved as ``\ASCII'' or ``plain text''.
244
245
246\subsubsection{Editors with Python modes}
247
248Several text editors have additional features that add functionality
249when you are creating a Python script. These can include coloring
250Python keywords to make your code easier to read, module browsing, or
251a built-in debugger. These include \program{Alpha}, \program{Pepper},
252and \program{BBedit}, and the MacPython IDE (Section \ref{IDE}).
253
254%\subsubsection{Alpha}
255% **NEED INFO HERE**
256
257\subsubsection{BBedit \label{scripting-with-BBedit}}
258
259If you use \program{BBEdit} to create your scripts you will want to tell it about the Python creator code so that
260you can simply double click on the saved file to launch it.
261\begin{itemize}
262 \item Launch \program{BBEdit}.
263 \item Select ``Preferences'' from the ``Edit'' menu.
264 \item Select ``File Types'' from the scrolling list.
265 \item click on the ``Add...'' button and navigate to
266 \program{PythonInterpreter} in the main directory of the
267 MacPython distribution; click ``open''.
268 \item Click on the ``Save'' button in the Preferences panel.
269\end{itemize}
270% Are there additional BBedit Python-specific features? I'm not aware of any.
271
272%\subsubsection{IDE}
273%You can use the \program{Python IDE} supplied in the MacPython Distribution to create longer Python scripts
274%-- see Section \ref{IDEwrite} for details.
275
276%\subsubsection{IDLE}
277%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
278%the standard documentation, but see Section \ref{TKInter} for guidance on using TKInter with MacPython.
279
280%\subsubsection{Pepper}
281% **NEED INFO HERE**
282
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000283\subsection{Configuration \label{configuration}}
284
285The MacPython distribution comes with \program{EditPythonPrefs}, an
286applet which will help you to customize the MacPython environment for
287your working habits.
288
289\subsubsection{EditPythonPrefs\label{EditPythonPrefs}}
290
291\program{EditPythonPrefs} gives you the capability to configure Python
292to behave the way you want it to. There are two ways to use
293\program{EditPythonPrefs}, you can use it to set the preferences in
294general, or you can drop a particular Python engine onto it to
295customize only that version. The latter can be handy if, for example,
296you want to have a second copy of the \program{PythonInterpreter} that
297keeps the output window open on a normal exit even though you prefer
298to normally not work that way.
299
300To change the default preferences, simply double-click on
301\program{EditPythonPrefs}. To change the preferences only for one copy
302of the Interpreter, drop the icon for that copy onto
303\program{EditPythonPrefs}. You can also use \program{EditPythonPrefs}
304in this fashion to set the preferences of the \program{Python IDE} and
305any applets you create -- see section %s \ref{BuildApplet} and
306\ref{IDEapplet}.
307
308\subsubsection{Adding modules to the Module Search Path
309 \label{search-path}}
310
311When executing an \keyword{import} statement, Python looks for modules
312in places defined by the \member{sys.path} To edit the
313\member{sys.path} on a Mac, launch \program{EditPythonPrefs}, and
314enter them into the largish field at the top (one per line).
315
316Since MacPython defines a main Python directory, the easiest thing is
317to add folders to search within the main Python directory. To add a
318folder of scripts that you created called ``My Folder'' located in the
319main Python Folder, enter \samp{\$(PYTHON):My Folder} onto a new line.
320
321To add the Desktop under OS 9 or below, add
322\samp{StartupDriveName:Desktop Folder} on a new line.
323
324\subsubsection{Default startup options \label{defaults}}
325
326% I'm assuming that there exists some other documentation on the
327% rest of the options so I only go over a couple here.
328
329The ``Default startup options...'' button in the
330\program{EditPythonPrefs} dialog box gives you many options including
331the ability to keep the ``Output'' window open after the script
332terminates, and the ability to enter interactive mode after the
333termination of the run script. The latter can be very helpful if you
334want to examine the objects that were created during your script.
335
336%\section{Nifty Tools}
337%There are many other tools included with the MacPython
338%distribution. In addition to those discussed here, make
339%sure to check the \file{Mac} directory.
340
341%\subsection{BuildApplet \label{BuildApplet}}
342% **NEED INFO HERE**
343
344%\subsection{BuildApplication}
345% **NEED INFO HERE**
346
347%\section{TKInter on the Mac \label{TKInter}}
348
349%TKinter is installed by default with the MacPython distribution, but
350%you may need to add the \file{lib-tk} folder to the Python Path (see
351%section \ref{search-path}). Also, it is important that you do not
352%try to launch Tk from within the \program{Python IDE} because the two
353%event loops will collide -- always run a script which uses Tkinter
354%with the \program{PythonInterpreter} instead -- see section
355%\ref{interpreter}.
356
357%\section{CGI on the Mac with Python \label{CGI}}
358%**NEED INFO HERE**
Fred Drake45aa5732000-10-14 05:09:42 +0000359
360\section{The IDE\label{IDE}}
361
362The \program{Python IDE} (Integrated Development Environment) is a
363separate application that acts as a text editor for your Python code,
364a class browser, a graphical debugger, and more.
365
366
367\subsection{Using the ``Python Interactive'' window}
368
369Use this window like you would the \program{PythonInterpreter}, except
370that you cannot use the ``Drag and drop'' method above. Instead,
371dropping a script onto the \program{Python IDE} icon will open the
Fred Drake38080452001-11-01 19:48:01 +0000372file in a separate script window (which you can then execute manually
Fred Drake45aa5732000-10-14 05:09:42 +0000373-- see section \ref{IDEexecution}).
374
375
376\subsection{Writing a Python Script \label{IDEwrite}}
377
378In addition to using the \program{Python IDE} interactively, you can
379also type out a complete Python program, saving it incrementally, and
380execute it or smaller selections of it.
381
382You can create a new script, open a previously saved script, and save
383your currently open script by selecting the appropriate item in the
384``File'' menu. Dropping a Python script onto the
385\program{Python IDE} will open it for editting.
386
387If you try to open a script with the \program{Python IDE} but either
388can't locate it from the ``Open'' dialog box, or you get an error
389message like ``Can't open file of type ...'' see section
Fred Drake8e850ee2000-10-14 05:24:20 +0000390\ref{creator-code}.
Fred Drake45aa5732000-10-14 05:09:42 +0000391
392When the \program{Python IDE} saves a script, it uses the creator code
393settings which are available by clicking on the small black triangle
394on the top right of the document window, and selecting ``save
395options''. The default is to save the file with the \program{Python
396IDE} as the creator, this means that you can open the file for editing
397by simply double-clicking on its icon. You might want to change this
398behaviour so that it will be opened by the
399\program{PythonInterpreter}, and run. To do this simply choose
400``Python Interpreter'' from the ``save options''. Note that these
401options are associated with the \emph{file} not the application.
402
403
404\subsection{Executing a script from within the IDE
405 \label{IDEexecution}}
406
407You can run the script in the frontmost window of the \program{Python
408IDE} by hitting the run all button. You should be aware, however that
409if you use the Python convention \samp{if __name__ == "__main__":} the
410script will \emph{not} be ``__main__'' by default. To get that
411behaviour you must select the ``Run as __main__'' option from the
412small black triangle on the top right of the document window. Note
413that this option is associated with the \emph{file} not the
414application. It \emph{will} stay active after a save, however; to shut
415this feature off simply select it again.
416
417
418\subsection{``Save as'' versus ``Save as Applet''
419 \label{IDEapplet}}
420
421When you are done writing your Python script you have the option of
422saving it as an ``applet'' (by selecting ``Save as applet'' from the
423``File'' menu). This has a significant advantage in that you can drop
424files or folders onto it, to pass them to the applet the way
425command-line users would type them onto the command-line to pass them
426as arguments to the script. However, you should make sure to save the
Fred Drake38080452001-11-01 19:48:01 +0000427applet as a separate file, do not overwrite the script you are
Fred Drake45aa5732000-10-14 05:09:42 +0000428writing, because you will not be able to edit it again.
429
430Accessing the items passed to the applet via ``drag-and-drop'' is done
431using the standard \member{sys.argv} mechanism. See the general
432documentation for more
433% need to link to the appropriate place in non-Mac docs
434
435Note that saving a script as an applet will not make it runnable on a
436system without a Python installation.
437
438%\subsection{Debugger}
439% **NEED INFO HERE**
440
441%\subsection{Module Browser}
442% **NEED INFO HERE**
443
444%\subsection{Profiler}
445% **NEED INFO HERE**
446% end IDE
447
448%\subsection{The ``Scripts'' menu}
449% **NEED INFO HERE**
450