blob: ed91c769a9d13da82d54dfefb38d0a234647ea0c [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
15The Mac-specific modules are documented in :ref:`mac-specific-services`.
16
17Python on Mac OS 9 or earlier can be quite different from Python on Unix or
18Windows, but is beyond the scope of this manual, as that platform is no longer
19supported, starting with Python 2.4. See http://www.cwi.nl/~jack/macpython for
20installers for the latest 2.3 release for Mac OS 9 and related documentation.
21
22
23.. _getting-osx:
24
25Getting and Installing MacPython
26================================
27
Georg Brandl30a88fa2013-04-14 10:17:35 +020028Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, you
Georg Brandlc0f10f32008-09-09 20:28:31 +000029are invited to install the most recent version of Python from the Python website
Georg Brandl06f3b3b2014-10-29 08:36:35 +010030(https://www.python.org). A current "universal binary" build of Python, which
Georg Brandlc0f10f32008-09-09 20:28:31 +000031runs natively on the Mac's new Intel and legacy PPC CPU's, is available there.
Georg Brandl8ec7f652007-08-15 14:28:01 +000032
33What you get after installing is a number of things:
34
Georg Brandl30a88fa2013-04-14 10:17:35 +020035* A :file:`MacPython 2.7` folder in your :file:`Applications` folder. In here
Georg Brandl8ec7f652007-08-15 14:28:01 +000036 you find IDLE, the development environment that is a standard part of official
37 Python distributions; PythonLauncher, which handles double-clicking Python
38 scripts from the Finder; and the "Build Applet" tool, which allows you to
39 package Python scripts as standalone applications on your system.
40
41* A framework :file:`/Library/Frameworks/Python.framework`, which includes the
42 Python executable and libraries. The installer adds this location to your shell
43 path. To uninstall MacPython, you can simply remove these three things. A
44 symlink to the Python executable is placed in /usr/local/bin/.
45
46The Apple-provided build of Python is installed in
47:file:`/System/Library/Frameworks/Python.framework` and :file:`/usr/bin/python`,
48respectively. You should never modify or delete these, as they are
Georg Brandlc0f10f32008-09-09 20:28:31 +000049Apple-controlled and are used by Apple- or third-party software. Remember that
50if you choose to install a newer Python version from python.org, you will have
51two different but functional Python installations on your computer, so it will
52be important that your paths and usages are consistent with what you want to do.
Georg Brandl8ec7f652007-08-15 14:28:01 +000053
54IDLE includes a help menu that allows you to access Python documentation. If you
55are completely new to Python you should start reading the tutorial introduction
56in that document.
57
58If you are familiar with Python on other Unix platforms you should read the
59section on running Python scripts from the Unix shell.
60
61
62How to run a Python script
63--------------------------
64
65Your best way to get started with Python on Mac OS X is through the IDLE
66integrated development environment, see section :ref:`ide` and use the Help menu
67when the IDE is running.
68
69If you want to run Python scripts from the Terminal window command line or from
70the Finder you first need an editor to create your script. Mac OS X comes with a
71number of standard Unix command line editors, :program:`vim` and
72:program:`emacs` among them. If you want a more Mac-like editor,
73:program:`BBEdit` or :program:`TextWrangler` from Bare Bones Software (see
Georg Brandl97ae4662014-10-29 10:26:56 +010074http://www.barebones.com/products/bbedit/index.html) are good choices, as is
Serhiy Storchakab4905ef2016-05-07 10:50:12 +030075:program:`TextMate` (see https://macromates.com/). Other editors include
Georg Brandl8ec7f652007-08-15 14:28:01 +000076:program:`Gvim` (http://macvim.org) and :program:`Aquamacs`
Ezio Melotti425aa2e2010-04-05 12:51:45 +000077(http://aquamacs.org/).
Georg Brandl8ec7f652007-08-15 14:28:01 +000078
79To run your script from the Terminal window you must make sure that
80:file:`/usr/local/bin` is in your shell search path.
81
82To run your script from the Finder you have two options:
83
84* Drag it to :program:`PythonLauncher`
85
86* Select :program:`PythonLauncher` as the default application to open your
87 script (or any .py script) through the finder Info window and double-click it.
88 :program:`PythonLauncher` has various preferences to control how your script is
89 launched. Option-dragging allows you to change these for one invocation, or use
90 its Preferences menu to change things globally.
91
92
93.. _osx-gui-scripts:
94
95Running scripts with a GUI
96--------------------------
97
98With older versions of Python, there is one Mac OS X quirk that you need to be
99aware of: programs that talk to the Aqua window manager (in other words,
100anything that has a GUI) need to be run in a special way. Use :program:`pythonw`
101instead of :program:`python` to start such scripts.
102
Georg Brandl30a88fa2013-04-14 10:17:35 +0200103With Python 2.7, you can use either :program:`python` or :program:`pythonw`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104
105
106Configuration
107-------------
108
109Python on OS X honors all standard Unix environment variables such as
110:envvar:`PYTHONPATH`, but setting these variables for programs started from the
111Finder is non-standard as the Finder does not read your :file:`.profile` or
Zachary Waree0c8e262015-08-29 22:25:04 -0500112:file:`.cshrc` at startup. You need to create a file
113:file:`~/.MacOSX/environment.plist`. See Apple's Technical Document QA1067 for
114details.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000115
116For more information on installation Python packages in MacPython, see section
117:ref:`mac-package-manager`.
118
119
120.. _ide:
121
122The IDE
123=======
124
125MacPython ships with the standard IDLE development environment. A good
Georg Brandl3d0a6022010-06-27 10:51:44 +0000126introduction to using IDLE can be found at
Georg Brandl97ae4662014-10-29 10:26:56 +0100127https://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000128
129
130.. _mac-package-manager:
131
132Installing Additional Python Packages
133=====================================
134
135There are several methods to install additional Python packages:
136
Georg Brandl8ec7f652007-08-15 14:28:01 +0000137* Packages can be installed via the standard Python distutils mode (``python
138 setup.py install``).
139
Georg Brandl30a88fa2013-04-14 10:17:35 +0200140* Many packages can also be installed via the :program:`setuptools` extension
Georg Brandl97ae4662014-10-29 10:26:56 +0100141 or :program:`pip` wrapper, see https://pip.pypa.io/.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000142
143
144GUI Programming on the Mac
145==========================
146
147There are several options for building GUI applications on the Mac with Python.
148
149*PyObjC* is a Python binding to Apple's Objective-C/Cocoa framework, which is
150the foundation of most modern Mac development. Information on PyObjC is
Georg Brandl97ae4662014-10-29 10:26:56 +0100151available from https://pythonhosted.org/pyobjc/.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000152
153The standard Python GUI toolkit is :mod:`Tkinter`, based on the cross-platform
Serhiy Storchakab4905ef2016-05-07 10:50:12 +0300154Tk toolkit (https://www.tcl.tk). An Aqua-native version of Tk is bundled with OS
Georg Brandl8ec7f652007-08-15 14:28:01 +0000155X by Apple, and the latest version can be downloaded and installed from
Serhiy Storchakab4905ef2016-05-07 10:50:12 +0300156https://www.activestate.com; it can also be built from source.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000157
158*wxPython* is another popular cross-platform GUI toolkit that runs natively on
159Mac OS X. Packages and documentation are available from http://www.wxpython.org.
160
161*PyQt* is another popular cross-platform GUI toolkit that runs natively on Mac
162OS X. More information can be found at
Georg Brandl6e0b44e2016-02-26 19:37:12 +0100163https://riverbankcomputing.com/software/pyqt/intro.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000164
165
166Distributing Python Applications on the Mac
167===========================================
168
Georg Brandl30a88fa2013-04-14 10:17:35 +0200169The "Build Applet" tool that is placed in the MacPython 2.7 folder is fine for
Georg Brandl8ec7f652007-08-15 14:28:01 +0000170packaging small Python scripts on your own machine to run as a standard Mac
171application. This tool, however, is not robust enough to distribute Python
172applications to other users.
173
174The standard tool for deploying standalone Python applications on the Mac is
175:program:`py2app`. More information on installing and using py2app can be found
176at http://undefined.org/python/#py2app.
177
178
Georg Brandl8ec7f652007-08-15 14:28:01 +0000179Other Resources
180===============
181
182The MacPython mailing list is an excellent support resource for Python users and
183developers on the Mac:
184
Georg Brandl06f3b3b2014-10-29 08:36:35 +0100185https://www.python.org/community/sigs/current/pythonmac-sig/
Georg Brandl8ec7f652007-08-15 14:28:01 +0000186
187Another useful resource is the MacPython wiki:
188
Georg Brandl06f3b3b2014-10-29 08:36:35 +0100189https://wiki.python.org/moin/MacPython
Georg Brandl8ec7f652007-08-15 14:28:01 +0000190