• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2.. _using-on-mac:
3
4*********************
5Using Python on a Mac
6*********************
7
8:Author: Bob Savage <bobsavage@mac.com>
9
10
11Python on a Mac running macOS 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
15.. _getting-osx:
16
17Getting and Installing MacPython
18================================
19
20macOS since version 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
22website (https://www.python.org).  A current "universal binary" build of Python,
23which runs natively on the Mac's new Intel and legacy PPC CPU's, is available
24there.
25
26What you get after installing is a number of things:
27
28* A :file:`Python 3.9` folder in your :file:`Applications` folder. In here
29  you find IDLE, the development environment that is a standard part of official
30  Python distributions; and PythonLauncher, which handles double-clicking Python
31  scripts from the Finder.
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.  Remember that
42if you choose to install a newer Python version from python.org, you will have
43two different but functional Python installations on your computer, so it will
44be important that your paths and usages are consistent with what you want to do.
45
46IDLE includes a help menu that allows you to access Python documentation. If you
47are completely new to Python you should start reading the tutorial introduction
48in that document.
49
50If you are familiar with Python on other Unix platforms you should read the
51section on running Python scripts from the Unix shell.
52
53
54How to run a Python script
55--------------------------
56
57Your best way to get started with Python on macOS is through the IDLE
58integrated development environment, see section :ref:`ide` and use the Help menu
59when the IDE is running.
60
61If you want to run Python scripts from the Terminal window command line or from
62the Finder you first need an editor to create your script. macOS comes with a
63number of standard Unix command line editors, :program:`vim` and
64:program:`emacs` among them. If you want a more Mac-like editor,
65:program:`BBEdit` or :program:`TextWrangler` from Bare Bones Software (see
66http://www.barebones.com/products/bbedit/index.html) are good choices, as is
67:program:`TextMate` (see https://macromates.com/). Other editors include
68:program:`Gvim` (http://macvim-dev.github.io/macvim/) and :program:`Aquamacs`
69(http://aquamacs.org/).
70
71To run your script from the Terminal window you must make sure that
72:file:`/usr/local/bin` is in your shell search path.
73
74To run your script from the Finder you have two options:
75
76* Drag it to :program:`PythonLauncher`
77
78* Select :program:`PythonLauncher` as the default application to open your
79  script (or any .py script) through the finder Info window and double-click it.
80  :program:`PythonLauncher` has various preferences to control how your script is
81  launched. Option-dragging allows you to change these for one invocation, or use
82  its Preferences menu to change things globally.
83
84
85.. _osx-gui-scripts:
86
87Running scripts with a GUI
88--------------------------
89
90With older versions of Python, there is one macOS quirk that you need to be
91aware of: programs that talk to the Aqua window manager (in other words,
92anything that has a GUI) need to be run in a special way. Use :program:`pythonw`
93instead of :program:`python` to start such scripts.
94
95With Python 3.9, you can use either :program:`python` or :program:`pythonw`.
96
97
98Configuration
99-------------
100
101Python on macOS honors all standard Unix environment variables such as
102:envvar:`PYTHONPATH`, but setting these variables for programs started from the
103Finder is non-standard as the Finder does not read your :file:`.profile` or
104:file:`.cshrc` at startup. You need to create a file
105:file:`~/.MacOSX/environment.plist`. See Apple's Technical Document QA1067 for
106details.
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
118introduction to using IDLE can be found at
119http://www.hashcollision.org/hkn/python/idle_intro/index.html.
120
121
122.. _mac-package-manager:
123
124Installing Additional Python Packages
125=====================================
126
127There are several methods to install additional Python packages:
128
129* Packages can be installed via the standard Python distutils mode (``python
130  setup.py install``).
131
132* Many packages can also be installed via the :program:`setuptools` extension
133  or :program:`pip` wrapper, see https://pip.pypa.io/.
134
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
143available from https://pypi.org/project/pyobjc/.
144
145The standard Python GUI toolkit is :mod:`tkinter`, based on the cross-platform
146Tk toolkit (https://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
148https://www.activestate.com; it can also be built from source.
149
150*wxPython* is another popular cross-platform GUI toolkit that runs natively on
151macOS. Packages and documentation are available from https://www.wxpython.org.
152
153*PyQt* is another popular cross-platform GUI toolkit that runs natively on
154macOS. More information can be found at
155https://riverbankcomputing.com/software/pyqt/intro.
156
157
158Distributing Python Applications on the Mac
159===========================================
160
161The standard tool for deploying standalone Python applications on the Mac is
162:program:`py2app`. More information on installing and using py2app can be found
163at http://undefined.org/python/#py2app.
164
165
166Other Resources
167===============
168
169The MacPython mailing list is an excellent support resource for Python users and
170developers on the Mac:
171
172https://www.python.org/community/sigs/current/pythonmac-sig/
173
174Another useful resource is the MacPython wiki:
175
176https://wiki.python.org/moin/MacPython
177
178