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