Jack Jansen | 7aeba45 | 2003-02-12 09:58:33 +0000 | [diff] [blame] | 1 | \chapter{Using Python on a Mac OS 9 Macintosh \label{using}} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 2 | \sectionauthor{Bob Savage}{bobsavage@mac.com} |
| 3 | |
Jack Jansen | 7aeba45 | 2003-02-12 09:58:33 +0000 | [diff] [blame] | 4 | Using Python on a Mac OS 9 Macintosh can seem like something completely |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 5 | different than using it on a \UNIX-like or Windows system. Most of the |
| 6 | Python documentation, both the ``official'' documentation and |
| 7 | published books, describe only how Python is used on these systems, |
Jack Jansen | 7aeba45 | 2003-02-12 09:58:33 +0000 | [diff] [blame] | 8 | causing confusion for the new user of MacPython-OS9. This chapter gives a |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 9 | brief introduction to the specifics of using Python on a Macintosh. |
| 10 | |
Jack Jansen | 7aeba45 | 2003-02-12 09:58:33 +0000 | [diff] [blame] | 11 | Note that this chapter is mainly relevant to Mac OS 9: MacPython-OSX |
| 12 | is a superset of a normal unix Python. While MacPython-OS9 runs fine |
| 13 | on Mac OS X it is a better choice to use MacPython-OSX there. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 14 | |
Jack Jansen | 7aeba45 | 2003-02-12 09:58:33 +0000 | [diff] [blame] | 15 | The section on the IDE (see Section \ref{IDE}) is relevant to MacPython-OSX |
| 16 | too. |
| 17 | |
| 18 | \section{Getting and Installing MacPython-OS9 \label{getting}} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 19 | |
| 20 | The most recent release version as well as possible newer experimental |
| 21 | versions are best found at the MacPython page maintained by Jack |
Fred Drake | 5e31c3f | 2002-10-18 18:16:19 +0000 | [diff] [blame] | 22 | Jansen: \url{http://www.cwi.nl/\textasciitilde jack/macpython.html}. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | Please refer to the \file{README} included with your distribution for |
| 26 | the most up-to-date instructions. |
| 27 | |
| 28 | |
| 29 | \section{Entering the interactive Interpreter |
| 30 | \label{interpreter}} |
| 31 | |
| 32 | The interactive interpreter that you will see used in Python |
| 33 | documentation is started by double-clicking the |
| 34 | \program{PythonInterpreter} icon, which looks like a 16-ton weight |
| 35 | falling. You should see the version information and the |
Fred Drake | 2441366 | 2001-04-13 17:32:49 +0000 | [diff] [blame] | 36 | \samp{>\code{>}>~} prompt. Use it exactly as described in the |
| 37 | standard documentation. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | \section{How to run a Python script} |
| 41 | |
| 42 | There are several ways to run an existing Python script; two common |
| 43 | ways to run a Python script are ``drag and drop'' and ``double |
| 44 | clicking''. Other ways include running it from within the IDE (see |
| 45 | Section \ref{IDE}), or launching via AppleScript. |
| 46 | |
| 47 | |
| 48 | \subsection{Drag and drop} |
| 49 | |
| 50 | One of the easiest ways to launch a Python script is via ``Drag and |
| 51 | Drop''. This is just like launching a text file in the Finder by |
| 52 | ``dragging'' it over your word processor's icon and ``dropping'' it |
| 53 | there. Make sure that you use an icon referring to the |
| 54 | \program{PythonInterpreter}, not the \program{IDE} or \program{Idle} |
| 55 | icons which have different behaviour which is described below. |
| 56 | |
| 57 | Some things that might have gone wrong: |
| 58 | |
| 59 | \begin{itemize} |
| 60 | \item |
| 61 | A window flashes after dropping the script onto the |
| 62 | \program{PythonInterpreter}, but then disappears. Most likely this is a |
| 63 | configuration issue; your \program{PythonInterpreter} is setup to exit |
| 64 | immediately upon completion, but your script assumes that if it prints |
| 65 | something that text will stick around for a while. To fix this, see |
Fred Drake | 8e850ee | 2000-10-14 05:24:20 +0000 | [diff] [blame] | 66 | section \ref{defaults}. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 67 | |
| 68 | \item |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 69 | When you waved the script icon over the \program{PythonInterpreter}, |
| 70 | the \program{PythonInterpreter} icon did not hilight. Most likely the |
| 71 | Creator code and document type is unset (or set incorrectly) -- this |
| 72 | often happens when a file originates on a non-Mac computer. See |
Fred Drake | 8e850ee | 2000-10-14 05:24:20 +0000 | [diff] [blame] | 73 | section \ref{creator-code} for more details. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 74 | \end{itemize} |
| 75 | |
| 76 | |
| 77 | \subsection{Set Creator and Double Click \label{creator-code}} |
| 78 | |
| 79 | If the script that you want to launch has the appropriate Creator Code |
| 80 | and File Type you can simply double-click on the script to launch it. |
| 81 | To be ``double-clickable'' a file needs to be of type \samp{TEXT}, |
| 82 | with a creator code of \samp{Pyth}. |
| 83 | |
| 84 | Setting the creator code and filetype can be done with the IDE (see |
| 85 | sections \ref{IDEwrite} and \ref{IDEapplet}), with an editor with a |
| 86 | Python mode (\program{BBEdit}) -- see section |
| 87 | \ref{scripting-with-BBedit}, or with assorted other Mac utilities, but |
| 88 | a script (\file{fixfiletypes.py}) has been included in the MacPython |
| 89 | distribution, making it possible to set the proper Type and Creator |
| 90 | Codes with Python. |
| 91 | |
| 92 | The \file{fixfiletypes.py} script will change the file type and |
| 93 | creator codes for the indicated directory. To use |
| 94 | \file{fixfiletypes.py}: |
| 95 | |
| 96 | \begin{enumerate} |
| 97 | \item |
| 98 | Locate it in the \file{scripts} folder of the \file{Mac} folder of the |
| 99 | MacPython distribution. |
| 100 | |
| 101 | \item |
| 102 | Put all of the scripts that you want to fix in a folder with nothing |
| 103 | else in it. |
| 104 | |
| 105 | \item |
| 106 | Double-click on the \file{fixfiletypes.py} icon. |
| 107 | |
| 108 | \item |
| 109 | Navigate 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 Jansen | 7aeba45 | 2003-02-12 09:58:33 +0000 | [diff] [blame] | 117 | There are two ways to simulate command-line arguments with MacPython-OS9. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 118 | |
| 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 |
| 131 | If you save the script as an applet (see Section \ref{IDEapplet}), you |
| 132 | can also simulate some command-line arguments via |
| 133 | ``Drag-and-Drop''. In this case, the names of the files that were |
| 134 | dropped onto the applet will be appended to \code{sys.argv}, so that |
| 135 | it will appear to the script as though they had been typed on a |
| 136 | command line. As on \UNIX\ systems, the first item in \code{sys.srgv} is |
| 137 | the path to the applet, and the rest are the files dropped on the |
| 138 | applet. |
| 139 | \end{enumerate} |
| 140 | |
| 141 | |
| 142 | \section{Creating a Python script} |
| 143 | |
| 144 | Since Python scripts are simply text files, they can be created in any |
| 145 | way that text files can be created, but some special tools also exist |
| 146 | with extra features. |
| 147 | |
| 148 | |
| 149 | \subsection{In an editor} |
| 150 | |
| 151 | You can create a text file with any word processing program such as |
| 152 | \program{MSWord} or \program{AppleWorks} but you need to make sure |
| 153 | that the file is saved as ``\ASCII'' or ``plain text''. |
| 154 | |
| 155 | |
| 156 | \subsubsection{Editors with Python modes} |
| 157 | |
| 158 | Several text editors have additional features that add functionality |
| 159 | when you are creating a Python script. These can include coloring |
| 160 | Python keywords to make your code easier to read, module browsing, or |
| 161 | a built-in debugger. These include \program{Alpha}, \program{Pepper}, |
| 162 | and \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 | |
| 169 | If you use \program{BBEdit} to create your scripts you will want to tell it about the Python creator code so that |
| 170 | you 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 | |
| 196 | The \program{Python IDE} (Integrated Development Environment) is a |
| 197 | separate application that acts as a text editor for your Python code, |
| 198 | a class browser, a graphical debugger, and more. |
| 199 | |
| 200 | |
| 201 | \subsection{Using the ``Python Interactive'' window} |
| 202 | |
| 203 | Use this window like you would the \program{PythonInterpreter}, except |
| 204 | that you cannot use the ``Drag and drop'' method above. Instead, |
| 205 | dropping a script onto the \program{Python IDE} icon will open the |
Fred Drake | 3808045 | 2001-11-01 19:48:01 +0000 | [diff] [blame] | 206 | file in a separate script window (which you can then execute manually |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 207 | -- see section \ref{IDEexecution}). |
| 208 | |
| 209 | |
| 210 | \subsection{Writing a Python Script \label{IDEwrite}} |
| 211 | |
| 212 | In addition to using the \program{Python IDE} interactively, you can |
| 213 | also type out a complete Python program, saving it incrementally, and |
| 214 | execute it or smaller selections of it. |
| 215 | |
| 216 | You can create a new script, open a previously saved script, and save |
| 217 | your 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 | |
| 221 | If you try to open a script with the \program{Python IDE} but either |
| 222 | can't locate it from the ``Open'' dialog box, or you get an error |
| 223 | message like ``Can't open file of type ...'' see section |
Fred Drake | 8e850ee | 2000-10-14 05:24:20 +0000 | [diff] [blame] | 224 | \ref{creator-code}. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 225 | |
| 226 | When the \program{Python IDE} saves a script, it uses the creator code |
| 227 | settings which are available by clicking on the small black triangle |
| 228 | on the top right of the document window, and selecting ``save |
| 229 | options''. The default is to save the file with the \program{Python |
| 230 | IDE} as the creator, this means that you can open the file for editing |
| 231 | by simply double-clicking on its icon. You might want to change this |
| 232 | behaviour 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 |
| 235 | options are associated with the \emph{file} not the application. |
| 236 | |
| 237 | |
| 238 | \subsection{Executing a script from within the IDE |
| 239 | \label{IDEexecution}} |
| 240 | |
| 241 | You can run the script in the frontmost window of the \program{Python |
| 242 | IDE} by hitting the run all button. You should be aware, however that |
| 243 | if you use the Python convention \samp{if __name__ == "__main__":} the |
| 244 | script will \emph{not} be ``__main__'' by default. To get that |
| 245 | behaviour you must select the ``Run as __main__'' option from the |
| 246 | small black triangle on the top right of the document window. Note |
| 247 | that this option is associated with the \emph{file} not the |
| 248 | application. It \emph{will} stay active after a save, however; to shut |
| 249 | this feature off simply select it again. |
| 250 | |
| 251 | |
| 252 | \subsection{``Save as'' versus ``Save as Applet'' |
| 253 | \label{IDEapplet}} |
| 254 | |
| 255 | When you are done writing your Python script you have the option of |
| 256 | saving it as an ``applet'' (by selecting ``Save as applet'' from the |
| 257 | ``File'' menu). This has a significant advantage in that you can drop |
| 258 | files or folders onto it, to pass them to the applet the way |
| 259 | command-line users would type them onto the command-line to pass them |
| 260 | as arguments to the script. However, you should make sure to save the |
Fred Drake | 3808045 | 2001-11-01 19:48:01 +0000 | [diff] [blame] | 261 | applet as a separate file, do not overwrite the script you are |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 262 | writing, because you will not be able to edit it again. |
| 263 | |
| 264 | Accessing the items passed to the applet via ``drag-and-drop'' is done |
| 265 | using the standard \member{sys.argv} mechanism. See the general |
| 266 | documentation for more |
| 267 | % need to link to the appropriate place in non-Mac docs |
| 268 | |
| 269 | Note that saving a script as an applet will not make it runnable on a |
| 270 | system 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 | |
| 287 | The MacPython distribution comes with \program{EditPythonPrefs}, an |
| 288 | applet which will help you to customize the MacPython environment for |
| 289 | your working habits. |
| 290 | |
| 291 | \subsection{EditPythonPrefs\label{EditPythonPrefs}} |
| 292 | |
| 293 | \program{EditPythonPrefs} gives you the capability to configure Python |
| 294 | to 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 |
| 296 | general, or you can drop a particular Python engine onto it to |
| 297 | customize only that version. The latter can be handy if, for example, |
| 298 | you want to have a second copy of the \program{PythonInterpreter} that |
| 299 | keeps the output window open on a normal exit even though you prefer |
| 300 | to normally not work that way. |
| 301 | |
| 302 | To change the default preferences, simply double-click on |
| 303 | \program{EditPythonPrefs}. To change the preferences only for one copy |
| 304 | of the Interpreter, drop the icon for that copy onto |
| 305 | \program{EditPythonPrefs}. You can also use \program{EditPythonPrefs} |
| 306 | in this fashion to set the preferences of the \program{Python IDE} and |
Fred Drake | 8e850ee | 2000-10-14 05:24:20 +0000 | [diff] [blame] | 307 | any applets you create -- see section %s \ref{BuildApplet} and |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 308 | \ref{IDEapplet}. |
| 309 | |
| 310 | \subsection{Adding modules to the Module Search Path |
| 311 | \label{search-path}} |
| 312 | |
| 313 | When executing an \keyword{import} statement, Python looks for modules |
| 314 | in places defined by the \member{sys.path} To edit the |
| 315 | \member{sys.path} on a Mac, launch \program{EditPythonPrefs}, and |
| 316 | enter them into the largish field at the top (one per line). |
| 317 | |
| 318 | Since MacPython defines a main Python directory, the easiest thing is |
| 319 | to add folders to search within the main Python directory. To add a |
| 320 | folder of scripts that you created called ``My Folder'' located in the |
| 321 | main Python Folder, enter \samp{\$(PYTHON):My Folder} onto a new line. |
| 322 | |
| 323 | To 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 | |
| 331 | The ``Default startup options...'' button in the |
| 332 | \program{EditPythonPrefs} dialog box gives you many options including |
| 333 | the ability to keep the ``Output'' window open after the script |
| 334 | terminates, and the ability to enter interactive mode after the |
| 335 | termination of the run script. The latter can be very helpful if you |
| 336 | want 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** |