blob: ca522c69fdf0dd740f4c4c3bdbd204226716dea0 [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
Guido van Rossumd8faa362007-04-27 19:54:29 +00005Python on any other \UNIX{} platform, but there are a number of additional
Brett Cannon7706c2d2005-02-13 22:50:04 +00006features 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
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009\UNIX{} or Windows, but is beyond the scope of this manual, as that platform
Brett Cannon7706c2d2005-02-13 22:50:04 +000010is 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
Guido van Rossumd8faa362007-04-27 19:54:29 +000016Mac OS X 10.4 comes with Python 2.3 pre-installed by Apple. However, you are
17encouraged to install the most recent version of Python from the Python website
18(\url{http://www.python.org}). A ``universal binary'' build of Python 2.5, which
19runs natively on the Mac's new Intel and legacy PPC CPU's, is available there.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000020
21What you get after installing is a number of things:
22
23\begin{itemize}
Guido van Rossumd8faa362007-04-27 19:54:29 +000024\item A \file{MacPython 2.5} folder in your \file{Applications} folder. In here
25 you find IDLE, the development environment that is a standard part of official
26 Python distributions; PythonLauncher, which handles double-clicking Python
27 scripts from the Finder; and the ``Build Applet'' tool, which allows you to
28 package Python scripts as standalone applications on your system.
29
30\item A framework \file{/Library/Frameworks/Python.framework}, which includes
31 the Python executable and libraries. The installer adds this location to your
32 shell path. To uninstall MacPython, you can simply remove these three
33 things. A symlink to the Python executable is placed in /usr/local/bin/.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000034\end{itemize}
35
Guido van Rossumd8faa362007-04-27 19:54:29 +000036The Apple-provided build of Python is installed in
37\file{/System/Library/Frameworks/Python.framework} and \file{/usr/bin/python},
38respectively. You should never modify or delete these, as they are
39Apple-controlled and are used by Apple- or third-party software.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000040
Guido van Rossumd8faa362007-04-27 19:54:29 +000041IDLE includes a help menu that allows you to access Python documentation. If you
42are completely new to Python you should start reading the tutorial introduction
43in that document.
Brett Cannon7706c2d2005-02-13 22:50:04 +000044
Guido van Rossumd8faa362007-04-27 19:54:29 +000045If you are familiar with Python on other \UNIX{} platforms you should read the
46section on running Python scripts from the \UNIX{} shell.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000047
Jack Jansend5e0a5a2003-04-09 15:12:38 +000048
49\subsection{How to run a Python script}
50
Guido van Rossumd8faa362007-04-27 19:54:29 +000051Your best way to get started with Python on Mac OS X is through the IDLE
52integrated development environment, see section \ref{IDE} and use the Help menu
53when the IDE is running.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000054
Guido van Rossumd8faa362007-04-27 19:54:29 +000055If you want to run Python scripts from the Terminal window command line or from
56the Finder you first need an editor to create your script. Mac OS X comes with a
57number of standard \UNIX{} command line editors, \program{vim} and
58\program{emacs} among them. If you want a more Mac-like editor, \program{BBEdit}
59or \program{TextWrangler} from Bare Bones Software (see
60\url{http://www.barebones.com/products/bbedit/index.shtml}) are good choices, as
61is \program{TextMate} (see \url{http://macromates.com/}). Other editors include
62\program{Gvim} (\url{http://macvim.org}) and \program{Aquamacs}
63(\url{http://aquamacs.org}).
Jack Jansend5e0a5a2003-04-09 15:12:38 +000064
65To run your script from the Terminal window you must make sure that
Guido van Rossumd8faa362007-04-27 19:54:29 +000066\file{/usr/local/bin} is in your shell search path.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000067
68To run your script from the Finder you have two options:
Guido van Rossumd8faa362007-04-27 19:54:29 +000069
Jack Jansend5e0a5a2003-04-09 15:12:38 +000070\begin{itemize}
Guido van Rossumd8faa362007-04-27 19:54:29 +000071\item Drag it to \program{PythonLauncher}
72\item Select \program{PythonLauncher} as the default application to open your
73 script (or any .py script) through the finder Info window and double-click it.
74 \program{PythonLauncher} has various preferences to control how your script is
75 launched. Option-dragging allows you to change these for one invocation, or
76 use its Preferences menu to change things globally.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000077\end{itemize}
78
Jack Jansend5e0a5a2003-04-09 15:12:38 +000079
Jack Jansenbae5c962003-04-11 15:35:28 +000080\subsection{Running scripts with a GUI \label{osx-gui-scripts}}
81
Guido van Rossumd8faa362007-04-27 19:54:29 +000082With older versions of Python, there is one Mac OS X quirk that you need to be
83aware of: programs that talk to the Aqua window manager (in other words,
84anything that has a GUI) need to be run in a special way. Use \program{pythonw}
85instead of \program{python} to start such scripts.
Jack Jansenbae5c962003-04-11 15:35:28 +000086
Guido van Rossumd8faa362007-04-27 19:54:29 +000087With Python 2.5, you can use either \program{python} or \program{pythonw}.
Jack Jansend5e0a5a2003-04-09 15:12:38 +000088
Guido van Rossumd8faa362007-04-27 19:54:29 +000089\subsection{Configuration}
Jack Jansend5e0a5a2003-04-09 15:12:38 +000090
Guido van Rossumd8faa362007-04-27 19:54:29 +000091Python on OS X honors all standard \UNIX{} environment variables such as
92\envvar{PYTHONPATH}, but setting these variables for programs started from the
93Finder is non-standard as the Finder does not read your \file{.profile} or
94\file{.cshrc} at startup. You need to create a file \file{\textasciitilde
95 /.MacOSX/environment.plist}. See Apple's Technical Document QA1067 for
96details.
97
98For more information on installation Python packages in MacPython, see section
99\ref{mac-package-manager}, ``Installing Additional Python Packages.''
Jack Jansend5e0a5a2003-04-09 15:12:38 +0000100
Fred Drake45aa5732000-10-14 05:09:42 +0000101
102\section{The IDE\label{IDE}}
103
Guido van Rossumd8faa362007-04-27 19:54:29 +0000104MacPython ships with the standard IDLE development environment. A good
105introduction to using IDLE can be found at
106\url{http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html}.
Fred Drake45aa5732000-10-14 05:09:42 +0000107
108
Guido van Rossumd8faa362007-04-27 19:54:29 +0000109\section{Installing Additional Python Packages \label{mac-package-manager}}
Fred Drake45aa5732000-10-14 05:09:42 +0000110
Guido van Rossumd8faa362007-04-27 19:54:29 +0000111There are several methods to install additional Python packages:
Fred Drake45aa5732000-10-14 05:09:42 +0000112
Guido van Rossumd8faa362007-04-27 19:54:29 +0000113\begin{itemize}
114\item \url{http://pythonmac.org/packages/} contains selected compiled packages
115 for Python 2.5, 2.4, and 2.3.
116\item Packages can be installed via the standard Python distutils mode
117 (\samp{python setup.py install}).
118\item Many packages can also be installed via the \program{setuptools}
119 extension.
120\end{itemize}
Fred Drake45aa5732000-10-14 05:09:42 +0000121
Fred Drake45aa5732000-10-14 05:09:42 +0000122
Guido van Rossumd8faa362007-04-27 19:54:29 +0000123\section{GUI Programming on the Mac}
Fred Drake45aa5732000-10-14 05:09:42 +0000124
Guido van Rossumd8faa362007-04-27 19:54:29 +0000125There are several options for building GUI applications on the Mac with Python.
Fred Drake45aa5732000-10-14 05:09:42 +0000126
Guido van Rossumd8faa362007-04-27 19:54:29 +0000127\emph{PyObjC} is a Python binding to Apple's Objective-C/Cocoa framework, which
128is the foundation of most modern Mac development. Information on PyObjC is
129available from \url{http://pybojc.sourceforge.net}.
Fred Drake45aa5732000-10-14 05:09:42 +0000130
Guido van Rossumd8faa362007-04-27 19:54:29 +0000131The standard Python GUI toolkit is \module{Tkinter}, based on the cross-platform
132Tk toolkit (\url{http://www.tcl.tk}). An Aqua-native version of Tk is bundled
133with OS X by Apple, and the latest version can be downloaded and installed from
134\url{http://www.activestate.com}; it can also be built from source.
Brett Cannon7706c2d2005-02-13 22:50:04 +0000135
Guido van Rossumd8faa362007-04-27 19:54:29 +0000136\emph{wxPython} is another popular cross-platform GUI toolkit that runs natively
137on Mac OS X. Packages and documentation are available from
138\url{http://www.wxpython.org}.
Brett Cannon7706c2d2005-02-13 22:50:04 +0000139
Guido van Rossumd8faa362007-04-27 19:54:29 +0000140\emph{PyQt} is another popular cross-platform GUI toolkit that runs natively on
141Mac OS X. More information can be found at
142\url{http://www.riverbankcomputing.co.uk/pyqt/}.
Brett Cannon7706c2d2005-02-13 22:50:04 +0000143
Brett Cannon7706c2d2005-02-13 22:50:04 +0000144
Guido van Rossumd8faa362007-04-27 19:54:29 +0000145\section{Distributing Python Applications on the Mac}
Brett Cannon7706c2d2005-02-13 22:50:04 +0000146
Guido van Rossumd8faa362007-04-27 19:54:29 +0000147The ``Build Applet'' tool that is placed in the MacPython 2.5 folder is fine for
148packaging small Python scripts on your own machine to run as a standard Mac
149application. This tool, however, is not robust enough to distribute Python
150applications to other users.
Brett Cannon7706c2d2005-02-13 22:50:04 +0000151
Guido van Rossumd8faa362007-04-27 19:54:29 +0000152The standard tool for deploying standalone Python applications on the Mac is
153\program{py2app}. More information on installing and using py2app can be found
154at \url{http://undefined.org/python/\#py2app}.
155
156\section{Application Scripting}
157
158Python can also be used to script other Mac applications via Apple's Open
159Scripting Architecture (OSA); see
160\url{http://appscript.sourceforge.net}. Appscript is a high-level, user-friendly
161Apple event bridge that allows you to control scriptable Mac OS X applications
162using ordinary Python scripts. Appscript makes Python a serious alternative to
163Apple's own \emph{AppleScript} language for automating your Mac. A related
164package, \emph{PyOSA}, is an OSA language component for the Python scripting
165language, allowing Python code to be executed by any OSA-enabled application
166(Script Editor, Mail, iTunes, etc.). PyOSA makes Python a full peer to
167AppleScript.
168
169\section{Other Resources}
170
171The MacPython mailing list is an excellent support resource for Python users and
172developers on the Mac:
173
174\url{http://www.python.org/community/sigs/current/pythonmac-sig/}
175
176Another useful resource is the MacPython wiki:
177
178\url{http://wiki.python.org/moin/MacPython}