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 | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 4 | Using Python on a Macintosh, especially on Mac OS 9 (MacPython-OSX |
Fred Drake | f1c9869 | 2003-07-22 01:09:22 +0000 | [diff] [blame] | 5 | includes a complete \UNIX{} Python) can seem like something completely |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 6 | different than using it on a \UNIX-like or Windows system. Most of the |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 7 | Python documentation, both the ``official'' documentation and published |
| 8 | books, describe only how Python is used on these systems, causing |
| 9 | confusion for the new user of MacPython-OS9. This chapter gives a brief |
| 10 | introduction to the specifics of using Python on a Macintosh. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 11 | |
| 12 | |
Jack Jansen | 7aeba45 | 2003-02-12 09:58:33 +0000 | [diff] [blame] | 13 | The section on the IDE (see Section \ref{IDE}) is relevant to MacPython-OSX |
| 14 | too. |
| 15 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 16 | \section{Getting and Installing MacPython-OSX \label{getting-OSX}} |
| 17 | |
| 18 | As of Python 2.3a2 the only sure way of getting MacPython-OSX on your machine |
| 19 | is getting a source distribution and building what is called a "framework Python". |
| 20 | The details are in the file \file{Mac/OSX/README}. |
| 21 | |
| 22 | As binary installers become available the details will be posted to |
| 23 | \url{http://www.cwi.nl/\textasciitilde jack/macpython.html}. |
| 24 | |
| 25 | |
| 26 | What 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 Drake | f1c9869 | 2003-07-22 01:09:22 +0000 | [diff] [blame] | 31 | PythonLauncher, which handles double-clicking Python scripts from |
| 32 | the Finder; and the Package Manager. |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 33 | |
Fred Drake | f1c9869 | 2003-07-22 01:09:22 +0000 | [diff] [blame] | 34 | \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 Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 37 | |
| 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 | |
| 42 | To uninstall MacPython you can simply remove these three things. |
| 43 | |
| 44 | PythonIDE contains an Apple Help Viewer book called "MacPython Help" |
| 45 | which you can access through its help menu. If you are completely new to |
| 46 | Python you should start reading the IDE introduction in that document. |
| 47 | |
Fred Drake | f1c9869 | 2003-07-22 01:09:22 +0000 | [diff] [blame] | 48 | If you are familiar with Python on other \UNIX{} platforms you should |
| 49 | read the section on running Python scripts from the \UNIX{} shell. |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 50 | |
| 51 | \subsection{How to run a Python script} |
| 52 | |
| 53 | Your best way to get started with Python on Mac OS X is through the PythonIDE |
| 54 | integrated development environment, see section \ref{IDE} and use the Help |
| 55 | menu when the IDE is running. |
| 56 | |
| 57 | If you want to run Python scripts from the Terminal window command line |
| 58 | or from the Finder you first need an editor to create your script. |
Fred Drake | f1c9869 | 2003-07-22 01:09:22 +0000 | [diff] [blame] | 59 | Mac OS X comes with a number of standard \UNIX{} command line editors, |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 60 | \program{vi} and \program{emacs} among them. If you want a more Mac-like |
| 61 | editor \program{BBEdit} or \program{TextWrangler} from Bare Bones Software |
Fred Drake | f1c9869 | 2003-07-22 01:09:22 +0000 | [diff] [blame] | 62 | (see \url{http://www.barebones.com/products/bbedit/index.shtml}) are |
| 63 | good choices. Their freeware \program{BBEdit Lite} is officially |
| 64 | discontinued but still available. \program{AppleWorks} or any other |
| 65 | word 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 Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 67 | |
| 68 | To 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}, |
| 70 | where the Apple-supplied Python lives (which is version 2.2, as of Mac OS X |
Jack Jansen | bae5c96 | 2003-04-11 15:35:28 +0000 | [diff] [blame] | 71 | 10.2.4). |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 72 | |
| 73 | To 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 | |
| 81 | PythonLauncher has various preferences to control how your script is launched. |
| 82 | Option-dragging allows you to change these for one invocation, or use its |
| 83 | Preferences menu to change things globally. |
| 84 | |
Jack Jansen | bae5c96 | 2003-04-11 15:35:28 +0000 | [diff] [blame] | 85 | \subsection{Running scripts with a GUI \label{osx-gui-scripts}} |
| 86 | |
| 87 | There is one Mac OS X quirk that you need to be aware of: programs |
| 88 | that talk to the Aqua window manager (in other words, anything that has a GUI) |
Neal Norwitz | 1ecbd67 | 2003-12-14 15:02:54 +0000 | [diff] [blame] | 89 | need to be run in a special way. Use \program{pythonw} instead of \program{python} |
Jack Jansen | bae5c96 | 2003-04-11 15:35:28 +0000 | [diff] [blame] | 90 | to start such scripts. |
| 91 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 92 | \subsection{configuration} |
| 93 | |
Fred Drake | f1c9869 | 2003-07-22 01:09:22 +0000 | [diff] [blame] | 94 | MacPython honours all standard \UNIX{} environment variables such as |
| 95 | \envvar{PYTHONPATH}, but setting these variables for programs started |
| 96 | from the Finder is non-standard |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 97 | as the Finder does not read your \file{.profile} or \file{.cshrc} at startup. |
| 98 | You need to create a file \file{\textasciitilde /.MacOSX/environment.plist}. |
| 99 | See Apple's Technical Document QA1067 for details. |
| 100 | |
| 101 | Installing additional Python packages is most easily done through the |
| 102 | Package Manager, see the MacPython Help Book for details. |
| 103 | |
Jack Jansen | 7aeba45 | 2003-02-12 09:58:33 +0000 | [diff] [blame] | 104 | \section{Getting and Installing MacPython-OS9 \label{getting}} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 105 | |
| 106 | The most recent release version as well as possible newer experimental |
| 107 | versions are best found at the MacPython page maintained by Jack |
Fred Drake | f1c9869 | 2003-07-22 01:09:22 +0000 | [diff] [blame] | 108 | Jansen: \url{http://homepages.cwi.nl/\textasciitilde jack/macpython.html}. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 109 | |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 110 | Please refer to the \file{README} included with your distribution for |
| 111 | the most up-to-date instructions. |
| 112 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 113 | Note that MacPython-OS9 runs fine on Mac OS X, and it runs in native |
| 114 | mode, not in the Classic environment. Unless you have specific |
| 115 | requirements for a CFM-based Python there is no reason not to |
| 116 | use MacPython-OSX, though. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 117 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 118 | |
| 119 | \subsection{Entering the interactive Interpreter |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 120 | \label{interpreter}} |
| 121 | |
| 122 | The interactive interpreter that you will see used in Python |
| 123 | documentation is started by double-clicking the |
| 124 | \program{PythonInterpreter} icon, which looks like a 16-ton weight |
| 125 | falling. You should see the version information and the |
Fred Drake | 2441366 | 2001-04-13 17:32:49 +0000 | [diff] [blame] | 126 | \samp{>\code{>}>~} prompt. Use it exactly as described in the |
| 127 | standard documentation. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 128 | |
| 129 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 130 | \subsection{How to run a Python script} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 131 | |
| 132 | There are several ways to run an existing Python script; two common |
| 133 | ways to run a Python script are ``drag and drop'' and ``double |
| 134 | clicking''. Other ways include running it from within the IDE (see |
| 135 | Section \ref{IDE}), or launching via AppleScript. |
| 136 | |
| 137 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 138 | \subsubsection{Drag and drop} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 139 | |
| 140 | One of the easiest ways to launch a Python script is via ``Drag and |
| 141 | Drop''. This is just like launching a text file in the Finder by |
| 142 | ``dragging'' it over your word processor's icon and ``dropping'' it |
| 143 | there. Make sure that you use an icon referring to the |
| 144 | \program{PythonInterpreter}, not the \program{IDE} or \program{Idle} |
| 145 | icons which have different behaviour which is described below. |
| 146 | |
| 147 | Some things that might have gone wrong: |
| 148 | |
| 149 | \begin{itemize} |
| 150 | \item |
| 151 | A window flashes after dropping the script onto the |
| 152 | \program{PythonInterpreter}, but then disappears. Most likely this is a |
| 153 | configuration issue; your \program{PythonInterpreter} is setup to exit |
| 154 | immediately upon completion, but your script assumes that if it prints |
| 155 | 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] | 156 | section \ref{defaults}. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 157 | |
| 158 | \item |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 159 | When you waved the script icon over the \program{PythonInterpreter}, |
| 160 | the \program{PythonInterpreter} icon did not hilight. Most likely the |
| 161 | Creator code and document type is unset (or set incorrectly) -- this |
| 162 | often happens when a file originates on a non-Mac computer. See |
Fred Drake | 8e850ee | 2000-10-14 05:24:20 +0000 | [diff] [blame] | 163 | section \ref{creator-code} for more details. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 164 | \end{itemize} |
| 165 | |
| 166 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 167 | \subsubsection{Set Creator and Double Click \label{creator-code}} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 168 | |
| 169 | If the script that you want to launch has the appropriate Creator Code |
| 170 | and File Type you can simply double-click on the script to launch it. |
| 171 | To be ``double-clickable'' a file needs to be of type \samp{TEXT}, |
| 172 | with a creator code of \samp{Pyth}. |
| 173 | |
| 174 | Setting the creator code and filetype can be done with the IDE (see |
| 175 | sections \ref{IDEwrite} and \ref{IDEapplet}), with an editor with a |
| 176 | Python mode (\program{BBEdit}) -- see section |
| 177 | \ref{scripting-with-BBedit}, or with assorted other Mac utilities, but |
| 178 | a script (\file{fixfiletypes.py}) has been included in the MacPython |
| 179 | distribution, making it possible to set the proper Type and Creator |
| 180 | Codes with Python. |
| 181 | |
| 182 | The \file{fixfiletypes.py} script will change the file type and |
| 183 | creator codes for the indicated directory. To use |
| 184 | \file{fixfiletypes.py}: |
| 185 | |
| 186 | \begin{enumerate} |
| 187 | \item |
| 188 | Locate it in the \file{scripts} folder of the \file{Mac} folder of the |
| 189 | MacPython distribution. |
| 190 | |
| 191 | \item |
| 192 | Put all of the scripts that you want to fix in a folder with nothing |
| 193 | else in it. |
| 194 | |
| 195 | \item |
| 196 | Double-click on the \file{fixfiletypes.py} icon. |
| 197 | |
| 198 | \item |
| 199 | Navigate into the folder of files you want to fix, and press the |
| 200 | ``Select current folder'' button. |
| 201 | \end{enumerate} |
| 202 | |
| 203 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 204 | \subsection{Simulating command line arguments |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 205 | \label{argv}} |
| 206 | |
Jack Jansen | 7aeba45 | 2003-02-12 09:58:33 +0000 | [diff] [blame] | 207 | There are two ways to simulate command-line arguments with MacPython-OS9. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 208 | |
| 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 |
| 221 | If you save the script as an applet (see Section \ref{IDEapplet}), you |
| 222 | can also simulate some command-line arguments via |
| 223 | ``Drag-and-Drop''. In this case, the names of the files that were |
| 224 | dropped onto the applet will be appended to \code{sys.argv}, so that |
| 225 | it will appear to the script as though they had been typed on a |
| 226 | command line. As on \UNIX\ systems, the first item in \code{sys.srgv} is |
| 227 | the path to the applet, and the rest are the files dropped on the |
| 228 | applet. |
| 229 | \end{enumerate} |
| 230 | |
| 231 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 232 | \subsection{Creating a Python script} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 233 | |
| 234 | Since Python scripts are simply text files, they can be created in any |
| 235 | way that text files can be created, but some special tools also exist |
| 236 | with extra features. |
| 237 | |
| 238 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 239 | \subsubsection{In an editor} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 240 | |
| 241 | You can create a text file with any word processing program such as |
| 242 | \program{MSWord} or \program{AppleWorks} but you need to make sure |
| 243 | that the file is saved as ``\ASCII'' or ``plain text''. |
| 244 | |
| 245 | |
| 246 | \subsubsection{Editors with Python modes} |
| 247 | |
| 248 | Several text editors have additional features that add functionality |
| 249 | when you are creating a Python script. These can include coloring |
| 250 | Python keywords to make your code easier to read, module browsing, or |
| 251 | a built-in debugger. These include \program{Alpha}, \program{Pepper}, |
| 252 | and \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 | |
| 259 | If you use \program{BBEdit} to create your scripts you will want to tell it about the Python creator code so that |
| 260 | you 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 Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 283 | \subsection{Configuration \label{configuration}} |
| 284 | |
| 285 | The MacPython distribution comes with \program{EditPythonPrefs}, an |
| 286 | applet which will help you to customize the MacPython environment for |
| 287 | your working habits. |
| 288 | |
| 289 | \subsubsection{EditPythonPrefs\label{EditPythonPrefs}} |
| 290 | |
| 291 | \program{EditPythonPrefs} gives you the capability to configure Python |
| 292 | to 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 |
| 294 | general, or you can drop a particular Python engine onto it to |
| 295 | customize only that version. The latter can be handy if, for example, |
| 296 | you want to have a second copy of the \program{PythonInterpreter} that |
| 297 | keeps the output window open on a normal exit even though you prefer |
| 298 | to normally not work that way. |
| 299 | |
| 300 | To change the default preferences, simply double-click on |
| 301 | \program{EditPythonPrefs}. To change the preferences only for one copy |
| 302 | of the Interpreter, drop the icon for that copy onto |
| 303 | \program{EditPythonPrefs}. You can also use \program{EditPythonPrefs} |
| 304 | in this fashion to set the preferences of the \program{Python IDE} and |
| 305 | any 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 | |
| 311 | When executing an \keyword{import} statement, Python looks for modules |
| 312 | in places defined by the \member{sys.path} To edit the |
| 313 | \member{sys.path} on a Mac, launch \program{EditPythonPrefs}, and |
| 314 | enter them into the largish field at the top (one per line). |
| 315 | |
| 316 | Since MacPython defines a main Python directory, the easiest thing is |
| 317 | to add folders to search within the main Python directory. To add a |
| 318 | folder of scripts that you created called ``My Folder'' located in the |
| 319 | main Python Folder, enter \samp{\$(PYTHON):My Folder} onto a new line. |
| 320 | |
| 321 | To 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 | |
| 329 | The ``Default startup options...'' button in the |
| 330 | \program{EditPythonPrefs} dialog box gives you many options including |
| 331 | the ability to keep the ``Output'' window open after the script |
| 332 | terminates, and the ability to enter interactive mode after the |
| 333 | termination of the run script. The latter can be very helpful if you |
| 334 | want 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 Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 359 | |
| 360 | \section{The IDE\label{IDE}} |
| 361 | |
| 362 | The \program{Python IDE} (Integrated Development Environment) is a |
| 363 | separate application that acts as a text editor for your Python code, |
| 364 | a class browser, a graphical debugger, and more. |
| 365 | |
| 366 | |
| 367 | \subsection{Using the ``Python Interactive'' window} |
| 368 | |
| 369 | Use this window like you would the \program{PythonInterpreter}, except |
| 370 | that you cannot use the ``Drag and drop'' method above. Instead, |
| 371 | dropping a script onto the \program{Python IDE} icon will open the |
Fred Drake | 3808045 | 2001-11-01 19:48:01 +0000 | [diff] [blame] | 372 | file in a separate script window (which you can then execute manually |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 373 | -- see section \ref{IDEexecution}). |
| 374 | |
| 375 | |
| 376 | \subsection{Writing a Python Script \label{IDEwrite}} |
| 377 | |
| 378 | In addition to using the \program{Python IDE} interactively, you can |
| 379 | also type out a complete Python program, saving it incrementally, and |
| 380 | execute it or smaller selections of it. |
| 381 | |
| 382 | You can create a new script, open a previously saved script, and save |
| 383 | your 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 | |
| 387 | If you try to open a script with the \program{Python IDE} but either |
| 388 | can't locate it from the ``Open'' dialog box, or you get an error |
| 389 | message like ``Can't open file of type ...'' see section |
Fred Drake | 8e850ee | 2000-10-14 05:24:20 +0000 | [diff] [blame] | 390 | \ref{creator-code}. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 391 | |
| 392 | When the \program{Python IDE} saves a script, it uses the creator code |
| 393 | settings which are available by clicking on the small black triangle |
| 394 | on the top right of the document window, and selecting ``save |
| 395 | options''. The default is to save the file with the \program{Python |
| 396 | IDE} as the creator, this means that you can open the file for editing |
| 397 | by simply double-clicking on its icon. You might want to change this |
| 398 | behaviour 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 |
| 401 | options are associated with the \emph{file} not the application. |
| 402 | |
| 403 | |
| 404 | \subsection{Executing a script from within the IDE |
| 405 | \label{IDEexecution}} |
| 406 | |
| 407 | You can run the script in the frontmost window of the \program{Python |
| 408 | IDE} by hitting the run all button. You should be aware, however that |
| 409 | if you use the Python convention \samp{if __name__ == "__main__":} the |
| 410 | script will \emph{not} be ``__main__'' by default. To get that |
| 411 | behaviour you must select the ``Run as __main__'' option from the |
| 412 | small black triangle on the top right of the document window. Note |
| 413 | that this option is associated with the \emph{file} not the |
| 414 | application. It \emph{will} stay active after a save, however; to shut |
| 415 | this feature off simply select it again. |
| 416 | |
| 417 | |
| 418 | \subsection{``Save as'' versus ``Save as Applet'' |
| 419 | \label{IDEapplet}} |
| 420 | |
| 421 | When you are done writing your Python script you have the option of |
| 422 | saving it as an ``applet'' (by selecting ``Save as applet'' from the |
| 423 | ``File'' menu). This has a significant advantage in that you can drop |
| 424 | files or folders onto it, to pass them to the applet the way |
| 425 | command-line users would type them onto the command-line to pass them |
| 426 | 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] | 427 | applet as a separate file, do not overwrite the script you are |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 428 | writing, because you will not be able to edit it again. |
| 429 | |
| 430 | Accessing the items passed to the applet via ``drag-and-drop'' is done |
| 431 | using the standard \member{sys.argv} mechanism. See the general |
| 432 | documentation for more |
| 433 | % need to link to the appropriate place in non-Mac docs |
| 434 | |
| 435 | Note that saving a script as an applet will not make it runnable on a |
| 436 | system 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 | |