blob: 7da291450c0f050c5695cae32816325c2ab01a1f [file] [log] [blame]
Georg Brandl3c8ce772007-11-01 20:58:08 +00001
2.. _using-on-mac:
3
4***************************
5Using Python on a Macintosh
6***************************
7
8:Author: Bob Savage <bobsavage@mac.com>
9
10
11Python on a Macintosh running Mac OS X is in principle very similar to Python on
12any other Unix platform, but there are a number of additional features such as
13the IDE and the Package Manager that are worth pointing out.
14
Georg Brandl3c8ce772007-11-01 20:58:08 +000015.. _getting-osx:
16
17Getting and Installing MacPython
18================================
19
Georg Brandle6bcc912008-05-12 18:05:20 +000020Mac OS X 10.5 comes with Python 2.5 pre-installed by Apple. However, you are
Georg Brandl3c8ce772007-11-01 20:58:08 +000021encouraged to install the most recent version of Python from the Python website
22(http://www.python.org). A "universal binary" build of Python 2.5, which runs
23natively on the Mac's new Intel and legacy PPC CPU's, is available there.
24
25What you get after installing is a number of things:
26
27* A :file:`MacPython 2.5` folder in your :file:`Applications` folder. In here
28 you find IDLE, the development environment that is a standard part of official
29 Python distributions; PythonLauncher, which handles double-clicking Python
30 scripts from the Finder; and the "Build Applet" tool, which allows you to
31 package Python scripts as standalone applications on your system.
32
33* A framework :file:`/Library/Frameworks/Python.framework`, which includes the
34 Python executable and libraries. The installer adds this location to your shell
35 path. To uninstall MacPython, you can simply remove these three things. A
36 symlink to the Python executable is placed in /usr/local/bin/.
37
38The Apple-provided build of Python is installed in
39:file:`/System/Library/Frameworks/Python.framework` and :file:`/usr/bin/python`,
40respectively. You should never modify or delete these, as they are
41Apple-controlled and are used by Apple- or third-party software.
42
43IDLE includes a help menu that allows you to access Python documentation. If you
44are completely new to Python you should start reading the tutorial introduction
45in that document.
46
47If you are familiar with Python on other Unix platforms you should read the
48section on running Python scripts from the Unix shell.
49
50
51How to run a Python script
52--------------------------
53
54Your best way to get started with Python on Mac OS X is through the IDLE
55integrated development environment, see section :ref:`ide` and use the Help menu
56when the IDE is running.
57
58If you want to run Python scripts from the Terminal window command line or from
59the Finder you first need an editor to create your script. Mac OS X comes with a
60number of standard Unix command line editors, :program:`vim` and
61:program:`emacs` among them. If you want a more Mac-like editor,
62:program:`BBEdit` or :program:`TextWrangler` from Bare Bones Software (see
63http://www.barebones.com/products/bbedit/index.shtml) are good choices, as is
64:program:`TextMate` (see http://macromates.com/). Other editors include
65:program:`Gvim` (http://macvim.org) and :program:`Aquamacs`
66(http://aquamacs.org).
67
68To run your script from the Terminal window you must make sure that
69:file:`/usr/local/bin` is in your shell search path.
70
71To run your script from the Finder you have two options:
72
73* Drag it to :program:`PythonLauncher`
74
75* Select :program:`PythonLauncher` as the default application to open your
76 script (or any .py script) through the finder Info window and double-click it.
77 :program:`PythonLauncher` has various preferences to control how your script is
78 launched. Option-dragging allows you to change these for one invocation, or use
79 its Preferences menu to change things globally.
80
81
82.. _osx-gui-scripts:
83
84Running scripts with a GUI
85--------------------------
86
87With older versions of Python, there is one Mac OS X quirk that you need to be
88aware of: programs that talk to the Aqua window manager (in other words,
89anything that has a GUI) need to be run in a special way. Use :program:`pythonw`
90instead of :program:`python` to start such scripts.
91
92With Python 2.5, you can use either :program:`python` or :program:`pythonw`.
93
94
95Configuration
96-------------
97
98Python on OS X honors all standard Unix environment variables such as
99:envvar:`PYTHONPATH`, but setting these variables for programs started from the
100Finder is non-standard as the Finder does not read your :file:`.profile` or
101:file:`.cshrc` at startup. You need to create a file :file:`~
102/.MacOSX/environment.plist`. See Apple's Technical Document QA1067 for details.
103
104For more information on installation Python packages in MacPython, see section
105:ref:`mac-package-manager`.
106
107
108.. _ide:
109
110The IDE
111=======
112
113MacPython ships with the standard IDLE development environment. A good
114introduction to using IDLE can be found at http://hkn.eecs.berkeley.edu/
115dyoo/python/idle_intro/index.html.
116
117
118.. _mac-package-manager:
119
120Installing Additional Python Packages
121=====================================
122
123There are several methods to install additional Python packages:
124
125* http://pythonmac.org/packages/ contains selected compiled packages for Python
126 2.5, 2.4, and 2.3.
127
128* Packages can be installed via the standard Python distutils mode (``python
129 setup.py install``).
130
131* Many packages can also be installed via the :program:`setuptools` extension.
132
133
134GUI Programming on the Mac
135==========================
136
137There are several options for building GUI applications on the Mac with Python.
138
139*PyObjC* is a Python binding to Apple's Objective-C/Cocoa framework, which is
140the foundation of most modern Mac development. Information on PyObjC is
141available from http://pyobjc.sourceforge.net.
142
143The standard Python GUI toolkit is :mod:`Tkinter`, based on the cross-platform
144Tk toolkit (http://www.tcl.tk). An Aqua-native version of Tk is bundled with OS
145X by Apple, and the latest version can be downloaded and installed from
146http://www.activestate.com; it can also be built from source.
147
148*wxPython* is another popular cross-platform GUI toolkit that runs natively on
149Mac OS X. Packages and documentation are available from http://www.wxpython.org.
150
151*PyQt* is another popular cross-platform GUI toolkit that runs natively on Mac
152OS X. More information can be found at
153http://www.riverbankcomputing.co.uk/pyqt/.
154
155
156Distributing Python Applications on the Mac
157===========================================
158
159The "Build Applet" tool that is placed in the MacPython 2.5 folder is fine for
160packaging small Python scripts on your own machine to run as a standard Mac
161application. This tool, however, is not robust enough to distribute Python
162applications to other users.
163
164The standard tool for deploying standalone Python applications on the Mac is
165:program:`py2app`. More information on installing and using py2app can be found
166at http://undefined.org/python/#py2app.
167
168
169Application Scripting
170=====================
171
172Python can also be used to script other Mac applications via Apple's Open
173Scripting Architecture (OSA); see http://appscript.sourceforge.net. Appscript is
174a high-level, user-friendly Apple event bridge that allows you to control
175scriptable Mac OS X applications using ordinary Python scripts. Appscript makes
176Python a serious alternative to Apple's own *AppleScript* language for
177automating your Mac. A related package, *PyOSA*, is an OSA language component
178for the Python scripting language, allowing Python code to be executed by any
179OSA-enabled application (Script Editor, Mail, iTunes, etc.). PyOSA makes Python
180a full peer to AppleScript.
181
182
183Other Resources
184===============
185
186The MacPython mailing list is an excellent support resource for Python users and
187developers on the Mac:
188
189http://www.python.org/community/sigs/current/pythonmac-sig/
190
191Another useful resource is the MacPython wiki:
192
193http://wiki.python.org/moin/MacPython
194