blob: ede864d7adedac9e6b3752fafdef743d075e92b8 [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 Brandle0959682013-04-14 10:17:35 +020020Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, you
21are invited to install the most recent version of Python 3 from the Python
Georg Brandle73778c2014-10-29 08:36:35 +010022website (https://www.python.org). A current "universal binary" build of Python,
Georg Brandle0959682013-04-14 10:17:35 +020023which runs natively on the Mac's new Intel and legacy PPC CPU's, is available
24there.
Georg Brandl3c8ce772007-11-01 20:58:08 +000025
26What you get after installing is a number of things:
27
Georg Brandl94a9a2d2013-04-14 10:19:31 +020028* A :file:`MacPython 3.4` folder in your :file:`Applications` folder. In here
Georg Brandl3c8ce772007-11-01 20:58:08 +000029 you find IDLE, the development environment that is a standard part of official
30 Python distributions; PythonLauncher, which handles double-clicking Python
31 scripts from the Finder; and the "Build Applet" tool, which allows you to
32 package Python scripts as standalone applications on your system.
33
34* A framework :file:`/Library/Frameworks/Python.framework`, which includes the
35 Python executable and libraries. The installer adds this location to your shell
36 path. To uninstall MacPython, you can simply remove these three things. A
37 symlink to the Python executable is placed in /usr/local/bin/.
38
39The Apple-provided build of Python is installed in
40:file:`/System/Library/Frameworks/Python.framework` and :file:`/usr/bin/python`,
41respectively. You should never modify or delete these, as they are
Benjamin Peterson9aa42992008-09-10 21:57:34 +000042Apple-controlled and are used by Apple- or third-party software. Remember that
43if you choose to install a newer Python version from python.org, you will have
44two different but functional Python installations on your computer, so it will
45be important that your paths and usages are consistent with what you want to do.
Georg Brandl3c8ce772007-11-01 20:58:08 +000046
47IDLE includes a help menu that allows you to access Python documentation. If you
48are completely new to Python you should start reading the tutorial introduction
49in that document.
50
51If you are familiar with Python on other Unix platforms you should read the
52section on running Python scripts from the Unix shell.
53
54
55How to run a Python script
56--------------------------
57
58Your best way to get started with Python on Mac OS X is through the IDLE
59integrated development environment, see section :ref:`ide` and use the Help menu
60when the IDE is running.
61
62If you want to run Python scripts from the Terminal window command line or from
63the Finder you first need an editor to create your script. Mac OS X comes with a
64number of standard Unix command line editors, :program:`vim` and
65:program:`emacs` among them. If you want a more Mac-like editor,
66:program:`BBEdit` or :program:`TextWrangler` from Bare Bones Software (see
Georg Brandl525d3552014-10-29 10:26:56 +010067http://www.barebones.com/products/bbedit/index.html) are good choices, as is
Georg Brandl3c8ce772007-11-01 20:58:08 +000068:program:`TextMate` (see http://macromates.com/). Other editors include
69:program:`Gvim` (http://macvim.org) and :program:`Aquamacs`
Ezio Melotti19192dd2010-04-05 13:25:51 +000070(http://aquamacs.org/).
Georg Brandl3c8ce772007-11-01 20:58:08 +000071
72To run your script from the Terminal window you must make sure that
73:file:`/usr/local/bin` is in your shell search path.
74
75To run your script from the Finder you have two options:
76
77* Drag it to :program:`PythonLauncher`
78
79* Select :program:`PythonLauncher` as the default application to open your
80 script (or any .py script) through the finder Info window and double-click it.
81 :program:`PythonLauncher` has various preferences to control how your script is
82 launched. Option-dragging allows you to change these for one invocation, or use
83 its Preferences menu to change things globally.
84
85
86.. _osx-gui-scripts:
87
88Running scripts with a GUI
89--------------------------
90
91With older versions of Python, there is one Mac OS X quirk that you need to be
92aware of: programs that talk to the Aqua window manager (in other words,
93anything that has a GUI) need to be run in a special way. Use :program:`pythonw`
94instead of :program:`python` to start such scripts.
95
Georg Brandl94a9a2d2013-04-14 10:19:31 +020096With Python 3.4, you can use either :program:`python` or :program:`pythonw`.
Georg Brandl3c8ce772007-11-01 20:58:08 +000097
98
99Configuration
100-------------
101
102Python on OS X honors all standard Unix environment variables such as
103:envvar:`PYTHONPATH`, but setting these variables for programs started from the
104Finder is non-standard as the Finder does not read your :file:`.profile` or
105:file:`.cshrc` at startup. You need to create a file :file:`~
106/.MacOSX/environment.plist`. See Apple's Technical Document QA1067 for details.
107
108For more information on installation Python packages in MacPython, see section
109:ref:`mac-package-manager`.
110
111
112.. _ide:
113
114The IDE
115=======
116
117MacPython ships with the standard IDLE development environment. A good
Benjamin Petersond7c3ed52010-06-27 22:32:30 +0000118introduction to using IDLE can be found at
Georg Brandl525d3552014-10-29 10:26:56 +0100119https://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html.
Georg Brandl3c8ce772007-11-01 20:58:08 +0000120
121
122.. _mac-package-manager:
123
124Installing Additional Python Packages
125=====================================
126
127There are several methods to install additional Python packages:
128
Georg Brandl3c8ce772007-11-01 20:58:08 +0000129* Packages can be installed via the standard Python distutils mode (``python
130 setup.py install``).
131
Georg Brandle0959682013-04-14 10:17:35 +0200132* Many packages can also be installed via the :program:`setuptools` extension
Georg Brandl525d3552014-10-29 10:26:56 +0100133 or :program:`pip` wrapper, see https://pip.pypa.io/.
Georg Brandl3c8ce772007-11-01 20:58:08 +0000134
135
136GUI Programming on the Mac
137==========================
138
139There are several options for building GUI applications on the Mac with Python.
140
141*PyObjC* is a Python binding to Apple's Objective-C/Cocoa framework, which is
142the foundation of most modern Mac development. Information on PyObjC is
Georg Brandl525d3552014-10-29 10:26:56 +0100143available from https://pythonhosted.org/pyobjc/.
Georg Brandl3c8ce772007-11-01 20:58:08 +0000144
Ezio Melotti1a263ad2010-03-14 09:51:37 +0000145The standard Python GUI toolkit is :mod:`tkinter`, based on the cross-platform
Georg Brandl3c8ce772007-11-01 20:58:08 +0000146Tk toolkit (http://www.tcl.tk). An Aqua-native version of Tk is bundled with OS
147X by Apple, and the latest version can be downloaded and installed from
148http://www.activestate.com; it can also be built from source.
149
150*wxPython* is another popular cross-platform GUI toolkit that runs natively on
151Mac OS X. Packages and documentation are available from http://www.wxpython.org.
152
153*PyQt* is another popular cross-platform GUI toolkit that runs natively on Mac
154OS X. More information can be found at
Ezio Melotti85232bb2009-09-14 00:51:48 +0000155http://www.riverbankcomputing.co.uk/software/pyqt/intro.
Georg Brandl3c8ce772007-11-01 20:58:08 +0000156
157
158Distributing Python Applications on the Mac
159===========================================
160
Georg Brandl94a9a2d2013-04-14 10:19:31 +0200161The "Build Applet" tool that is placed in the MacPython 3.4 folder is fine for
Georg Brandl3c8ce772007-11-01 20:58:08 +0000162packaging small Python scripts on your own machine to run as a standard Mac
163application. This tool, however, is not robust enough to distribute Python
164applications to other users.
165
166The standard tool for deploying standalone Python applications on the Mac is
167:program:`py2app`. More information on installing and using py2app can be found
168at http://undefined.org/python/#py2app.
169
170
Georg Brandl3c8ce772007-11-01 20:58:08 +0000171Other Resources
172===============
173
174The MacPython mailing list is an excellent support resource for Python users and
175developers on the Mac:
176
Georg Brandle73778c2014-10-29 08:36:35 +0100177https://www.python.org/community/sigs/current/pythonmac-sig/
Georg Brandl3c8ce772007-11-01 20:58:08 +0000178
179Another useful resource is the MacPython wiki:
180
Georg Brandle73778c2014-10-29 08:36:35 +0100181https://wiki.python.org/moin/MacPython
Georg Brandl3c8ce772007-11-01 20:58:08 +0000182