blob: bfa478e7f1a99913010e0c0cf26169c07c881f23 [file] [log] [blame]
Brett Cannon7706c2d2005-02-13 22:50:04 +00001\chapter{Using Python on a Macintosh \label{using}}
Fred Drake45aa5732000-10-14 05:09:42 +00002\sectionauthor{Bob Savage}{bobsavage@mac.com}
3
Brett Cannon7706c2d2005-02-13 22:50:04 +00004Python on a Macintosh running Mac OS X is in principle very similar to
5Python on any other \UNIX platform, but there are a number of additional
6features such as the IDE and the Package Manager that are worth pointing out.
Fred Drake45aa5732000-10-14 05:09:42 +00007
Brett Cannon7706c2d2005-02-13 22:50:04 +00008Python on Mac OS 9 or earlier can be quite different from Python on
9Unix or Windows, but is beyond the scope of this manual, as that platform
10is no longer supported, starting with Python 2.4. See
11\url{http://www.cwi.nl/\textasciitilde jack/macpython} for installers
12for the latest 2.3 release for Mac OS 9 and related documentation.
Fred Drake45aa5732000-10-14 05:09:42 +000013
Brett Cannon7706c2d2005-02-13 22:50:04 +000014\section{Getting and Installing MacPython \label{getting-OSX}}
Jack Jansen7aeba452003-02-12 09:58:33 +000015
Brett Cannon7706c2d2005-02-13 22:50:04 +000016Mac OS X 10.3 comes with Python 2.3 pre-installed by Apple.
17This installation does not come with the IDE and other additions, however,
18so to get these you need to install the \program{MacPython for Panther additions}
19from the MacPython website, \url{http://www.cwi.nl/\textasciitilde jack/macpython}.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000020
Brett Cannon7706c2d2005-02-13 22:50:04 +000021For MacPython 2.4, or for any MacPython on earlier releases of Mac OS X,
22you need to install a full distribution from the same website.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000023
24What you get after installing is a number of things:
25
26\begin{itemize}
27 \item A \file{MacPython-2.3} folder in your \file{Applications}
28 folder. In here you find the PythonIDE Integrated Development Environment;
Fred Drakef1c98692003-07-22 01:09:22 +000029 PythonLauncher, which handles double-clicking Python scripts from
30 the Finder; and the Package Manager.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000031
Fred Drakef1c98692003-07-22 01:09:22 +000032 \item A fairly standard \UNIX{} commandline Python interpreter in
33 \file{/usr/local/bin/python}, but without the usual
34 \file{/usr/local/lib/python}.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000035
36 \item A framework \file{/Library/Frameworks/Python.framework}, where
37 all the action really is, but which you usually do not have to be aware of.
38\end{itemize}
39
40To uninstall MacPython you can simply remove these three things.
41
Brett Cannon7706c2d2005-02-13 22:50:04 +000042If you use the ``additions'' installer to install on top of an existing
43Apple-Python you will not get the framework and the commandline interpreter,
44as they have been installed by Apple already, in
45\file{/System/Library/Frameworks/Python.framework} and
46\file{/usr/bin/python}, respectively. You should in principle never modify
47or delete these, as they are Apple-controlled and may be used by Apple- or
48third-party software.
49
Jack Jansend5e0a5a2003-04-09 15:12:38 +000050PythonIDE contains an Apple Help Viewer book called "MacPython Help"
51which you can access through its help menu. If you are completely new to
52Python you should start reading the IDE introduction in that document.
53
Fred Drakef1c98692003-07-22 01:09:22 +000054If you are familiar with Python on other \UNIX{} platforms you should
55read the section on running Python scripts from the \UNIX{} shell.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000056
57\subsection{How to run a Python script}
58
59Your best way to get started with Python on Mac OS X is through the PythonIDE
60integrated development environment, see section \ref{IDE} and use the Help
61menu when the IDE is running.
62
63If you want to run Python scripts from the Terminal window command line
64or from the Finder you first need an editor to create your script.
Fred Drakef1c98692003-07-22 01:09:22 +000065Mac OS X comes with a number of standard \UNIX{} command line editors,
Brett Cannon7706c2d2005-02-13 22:50:04 +000066\program{vim} and \program{emacs} among them. If you want a more Mac-like
Jack Jansend5e0a5a2003-04-09 15:12:38 +000067editor \program{BBEdit} or \program{TextWrangler} from Bare Bones Software
Fred Drakef1c98692003-07-22 01:09:22 +000068(see \url{http://www.barebones.com/products/bbedit/index.shtml}) are
Brett Cannon7706c2d2005-02-13 22:50:04 +000069good choices. \program{AppleWorks} or any other
Brett Cannonac7836a2004-12-24 02:40:59 +000070word processor that can save files in ASCII is also a possibility, including
71\program{TextEdit} which is included with OS X.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000072
73To run your script from the Terminal window you must make sure that
Brett Cannon7706c2d2005-02-13 22:50:04 +000074\file{/usr/local/bin} is in your shell search path.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000075
76To run your script from the Finder you have two options:
77\begin{itemize}
78 \item Drag it to \program{PythonLauncher}
79 \item Select \program{PythonLauncher} as the default application
80 to open your script (or any .py script) through the finder Info window
81 and double-click it.
82\end{itemize}
83
84PythonLauncher has various preferences to control how your script is launched.
85Option-dragging allows you to change these for one invocation, or use its
86Preferences menu to change things globally.
87
Jack Jansenbae5c962003-04-11 15:35:28 +000088\subsection{Running scripts with a GUI \label{osx-gui-scripts}}
89
90There is one Mac OS X quirk that you need to be aware of: programs
91that talk to the Aqua window manager (in other words, anything that has a GUI)
Neal Norwitz1ecbd672003-12-14 15:02:54 +000092need to be run in a special way. Use \program{pythonw} instead of \program{python}
Jack Jansenbae5c962003-04-11 15:35:28 +000093to start such scripts.
94
Jack Jansend5e0a5a2003-04-09 15:12:38 +000095\subsection{configuration}
96
Fred Drakef1c98692003-07-22 01:09:22 +000097MacPython honours all standard \UNIX{} environment variables such as
98\envvar{PYTHONPATH}, but setting these variables for programs started
99from the Finder is non-standard
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000100as the Finder does not read your \file{.profile} or \file{.cshrc} at startup.
101You need to create a file \file{\textasciitilde /.MacOSX/environment.plist}.
102See Apple's Technical Document QA1067 for details.
103
104Installing additional Python packages is most easily done through the
105Package Manager, see the MacPython Help Book for details.
106
Fred Drake45aa5732000-10-14 05:09:42 +0000107
108\section{The IDE\label{IDE}}
109
110The \program{Python IDE} (Integrated Development Environment) is a
111separate application that acts as a text editor for your Python code,
112a class browser, a graphical debugger, and more.
113
Brett Cannon7706c2d2005-02-13 22:50:04 +0000114The online Python Help contains a quick walkthrough of the IDE that
115shows the major features and how to use them.
Fred Drake45aa5732000-10-14 05:09:42 +0000116
117\subsection{Using the ``Python Interactive'' window}
118
Brett Cannon7706c2d2005-02-13 22:50:04 +0000119Use this window like you would use a normal \UNIX{} command line
120interpreter.
Fred Drake45aa5732000-10-14 05:09:42 +0000121
122\subsection{Writing a Python Script \label{IDEwrite}}
123
124In addition to using the \program{Python IDE} interactively, you can
125also type out a complete Python program, saving it incrementally, and
126execute it or smaller selections of it.
127
128You can create a new script, open a previously saved script, and save
129your currently open script by selecting the appropriate item in the
130``File'' menu. Dropping a Python script onto the
Raymond Hettinger565ea5a2004-10-02 11:02:59 +0000131\program{Python IDE} will open it for editing.
Fred Drake45aa5732000-10-14 05:09:42 +0000132
Fred Drake45aa5732000-10-14 05:09:42 +0000133When the \program{Python IDE} saves a script, it uses the creator code
134settings which are available by clicking on the small black triangle
135on the top right of the document window, and selecting ``save
136options''. The default is to save the file with the \program{Python
137IDE} as the creator, this means that you can open the file for editing
138by simply double-clicking on its icon. You might want to change this
139behaviour so that it will be opened by the
Brett Cannon7706c2d2005-02-13 22:50:04 +0000140\program{PythonLauncher}, and run. To do this simply choose
141``PythonLauncher'' from the ``save options''. Note that these
Fred Drake45aa5732000-10-14 05:09:42 +0000142options are associated with the \emph{file} not the application.
143
144
145\subsection{Executing a script from within the IDE
146 \label{IDEexecution}}
147
148You can run the script in the frontmost window of the \program{Python
149IDE} by hitting the run all button. You should be aware, however that
150if you use the Python convention \samp{if __name__ == "__main__":} the
151script will \emph{not} be ``__main__'' by default. To get that
152behaviour you must select the ``Run as __main__'' option from the
153small black triangle on the top right of the document window. Note
154that this option is associated with the \emph{file} not the
155application. It \emph{will} stay active after a save, however; to shut
156this feature off simply select it again.
157
158
159\subsection{``Save as'' versus ``Save as Applet''
160 \label{IDEapplet}}
161
162When you are done writing your Python script you have the option of
163saving it as an ``applet'' (by selecting ``Save as applet'' from the
164``File'' menu). This has a significant advantage in that you can drop
165files or folders onto it, to pass them to the applet the way
166command-line users would type them onto the command-line to pass them
167as arguments to the script. However, you should make sure to save the
Fred Drake38080452001-11-01 19:48:01 +0000168applet as a separate file, do not overwrite the script you are
Fred Drake45aa5732000-10-14 05:09:42 +0000169writing, because you will not be able to edit it again.
170
171Accessing the items passed to the applet via ``drag-and-drop'' is done
172using the standard \member{sys.argv} mechanism. See the general
173documentation for more
174% need to link to the appropriate place in non-Mac docs
175
176Note that saving a script as an applet will not make it runnable on a
177system without a Python installation.
178
179%\subsection{Debugger}
180% **NEED INFO HERE**
181
182%\subsection{Module Browser}
183% **NEED INFO HERE**
184
185%\subsection{Profiler}
186% **NEED INFO HERE**
187% end IDE
188
189%\subsection{The ``Scripts'' menu}
190% **NEED INFO HERE**
Brett Cannon7706c2d2005-02-13 22:50:04 +0000191
192\section{The Package Manager}
193
194Historically MacPython came with a number of useful extension packages
195included, because most Macintosh users do not have access to a development
196environment and C compiler. For Mac OS X that bundling is no longer done,
197but a new mechanism has been made available to allow easy access to
198extension packages.
199
200The Python Package Manager helps you installing additional packages
201that enhance Python. It determines the exact MacOS version and Python
202version you have and uses that information to download a database that
203has packages that are tested and tried on that combination. In other
204words: if something is in your Package Manager window but does not work
205you are free to blame the database maintainer.
206
207PackageManager then checks which of the packages you have installed and
208which ones are not. This should also work when you have installed packages
209outside of PackageManager. You can select packages and install them,
210and PackageManager will work out the requirements and install these too.
211
212Often PackageManager will list a package in two flavors: binary and
213source. Binary should always work, source will only work if you have
214installed the Apple Developer Tools. PackageManager will warn you about
215this, and also about other external dependencies.
216
217PackageManager is available as a separate application and also as a
218function of the IDE, through the File->Package Manager menu entry.