Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 1 | \chapter{Using Python on a Macintosh \label{using}} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 2 | \sectionauthor{Bob Savage}{bobsavage@mac.com} |
| 3 | |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 4 | Python on a Macintosh running Mac OS X is in principle very similar to |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5 | Python on any other \UNIX{} platform, but there are a number of additional |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 6 | features such as the IDE and the Package Manager that are worth pointing out. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 7 | |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 8 | Python on Mac OS 9 or earlier can be quite different from Python on |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 9 | \UNIX{} or Windows, but is beyond the scope of this manual, as that platform |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 10 | is no longer supported, starting with Python 2.4. See |
| 11 | \url{http://www.cwi.nl/\textasciitilde jack/macpython} for installers |
| 12 | for the latest 2.3 release for Mac OS 9 and related documentation. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 13 | |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 14 | \section{Getting and Installing MacPython \label{getting-OSX}} |
Jack Jansen | 7aeba45 | 2003-02-12 09:58:33 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 16 | Mac OS X 10.4 comes with Python 2.3 pre-installed by Apple. However, you are |
| 17 | encouraged 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 |
| 19 | runs natively on the Mac's new Intel and legacy PPC CPU's, is available there. |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 20 | |
| 21 | What you get after installing is a number of things: |
| 22 | |
| 23 | \begin{itemize} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 24 | \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 Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 34 | \end{itemize} |
| 35 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 36 | The Apple-provided build of Python is installed in |
| 37 | \file{/System/Library/Frameworks/Python.framework} and \file{/usr/bin/python}, |
| 38 | respectively. You should never modify or delete these, as they are |
| 39 | Apple-controlled and are used by Apple- or third-party software. |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 41 | IDLE includes a help menu that allows you to access Python documentation. If you |
| 42 | are completely new to Python you should start reading the tutorial introduction |
| 43 | in that document. |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 45 | If you are familiar with Python on other \UNIX{} platforms you should read the |
| 46 | section on running Python scripts from the \UNIX{} shell. |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 47 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 48 | |
| 49 | \subsection{How to run a Python script} |
| 50 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 51 | Your best way to get started with Python on Mac OS X is through the IDLE |
| 52 | integrated development environment, see section \ref{IDE} and use the Help menu |
| 53 | when the IDE is running. |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 55 | If you want to run Python scripts from the Terminal window command line or from |
| 56 | the Finder you first need an editor to create your script. Mac OS X comes with a |
| 57 | number of standard \UNIX{} command line editors, \program{vim} and |
| 58 | \program{emacs} among them. If you want a more Mac-like editor, \program{BBEdit} |
| 59 | or \program{TextWrangler} from Bare Bones Software (see |
| 60 | \url{http://www.barebones.com/products/bbedit/index.shtml}) are good choices, as |
| 61 | is \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 Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 64 | |
| 65 | To run your script from the Terminal window you must make sure that |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 66 | \file{/usr/local/bin} is in your shell search path. |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 67 | |
| 68 | To run your script from the Finder you have two options: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 69 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 70 | \begin{itemize} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 71 | \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 Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 77 | \end{itemize} |
| 78 | |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 79 | |
Jack Jansen | bae5c96 | 2003-04-11 15:35:28 +0000 | [diff] [blame] | 80 | \subsection{Running scripts with a GUI \label{osx-gui-scripts}} |
| 81 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 82 | With older versions of Python, there is one Mac OS X quirk that you need to be |
| 83 | aware of: programs that talk to the Aqua window manager (in other words, |
| 84 | anything that has a GUI) need to be run in a special way. Use \program{pythonw} |
| 85 | instead of \program{python} to start such scripts. |
Jack Jansen | bae5c96 | 2003-04-11 15:35:28 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 87 | With Python 2.5, you can use either \program{python} or \program{pythonw}. |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 89 | \subsection{Configuration} |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 91 | Python on OS X honors all standard \UNIX{} environment variables such as |
| 92 | \envvar{PYTHONPATH}, but setting these variables for programs started from the |
| 93 | Finder 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 |
| 96 | details. |
| 97 | |
| 98 | For more information on installation Python packages in MacPython, see section |
| 99 | \ref{mac-package-manager}, ``Installing Additional Python Packages.'' |
Jack Jansen | d5e0a5a | 2003-04-09 15:12:38 +0000 | [diff] [blame] | 100 | |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 101 | |
| 102 | \section{The IDE\label{IDE}} |
| 103 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 104 | MacPython ships with the standard IDLE development environment. A good |
| 105 | introduction to using IDLE can be found at |
| 106 | \url{http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html}. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 107 | |
| 108 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 109 | \section{Installing Additional Python Packages \label{mac-package-manager}} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 111 | There are several methods to install additional Python packages: |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 113 | \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 Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 121 | |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 123 | \section{GUI Programming on the Mac} |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 125 | There are several options for building GUI applications on the Mac with Python. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 127 | \emph{PyObjC} is a Python binding to Apple's Objective-C/Cocoa framework, which |
| 128 | is the foundation of most modern Mac development. Information on PyObjC is |
| 129 | available from \url{http://pybojc.sourceforge.net}. |
Fred Drake | 45aa573 | 2000-10-14 05:09:42 +0000 | [diff] [blame] | 130 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 131 | The standard Python GUI toolkit is \module{Tkinter}, based on the cross-platform |
| 132 | Tk toolkit (\url{http://www.tcl.tk}). An Aqua-native version of Tk is bundled |
| 133 | with 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 Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 136 | \emph{wxPython} is another popular cross-platform GUI toolkit that runs natively |
| 137 | on Mac OS X. Packages and documentation are available from |
| 138 | \url{http://www.wxpython.org}. |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 140 | \emph{PyQt} is another popular cross-platform GUI toolkit that runs natively on |
| 141 | Mac OS X. More information can be found at |
| 142 | \url{http://www.riverbankcomputing.co.uk/pyqt/}. |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 143 | |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 145 | \section{Distributing Python Applications on the Mac} |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 147 | The ``Build Applet'' tool that is placed in the MacPython 2.5 folder is fine for |
| 148 | packaging small Python scripts on your own machine to run as a standard Mac |
| 149 | application. This tool, however, is not robust enough to distribute Python |
| 150 | applications to other users. |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 152 | The standard tool for deploying standalone Python applications on the Mac is |
| 153 | \program{py2app}. More information on installing and using py2app can be found |
| 154 | at \url{http://undefined.org/python/\#py2app}. |
| 155 | |
| 156 | \section{Application Scripting} |
| 157 | |
| 158 | Python can also be used to script other Mac applications via Apple's Open |
| 159 | Scripting Architecture (OSA); see |
| 160 | \url{http://appscript.sourceforge.net}. Appscript is a high-level, user-friendly |
| 161 | Apple event bridge that allows you to control scriptable Mac OS X applications |
| 162 | using ordinary Python scripts. Appscript makes Python a serious alternative to |
| 163 | Apple's own \emph{AppleScript} language for automating your Mac. A related |
| 164 | package, \emph{PyOSA}, is an OSA language component for the Python scripting |
| 165 | language, allowing Python code to be executed by any OSA-enabled application |
| 166 | (Script Editor, Mail, iTunes, etc.). PyOSA makes Python a full peer to |
| 167 | AppleScript. |
| 168 | |
| 169 | \section{Other Resources} |
| 170 | |
| 171 | The MacPython mailing list is an excellent support resource for Python users and |
| 172 | developers on the Mac: |
| 173 | |
| 174 | \url{http://www.python.org/community/sigs/current/pythonmac-sig/} |
| 175 | |
| 176 | Another useful resource is the MacPython wiki: |
| 177 | |
| 178 | \url{http://wiki.python.org/moin/MacPython} |