1
2.. _using-on-mac:
3
4*********************
5Using Python on a Mac
6*********************
7
8:Author: Bob Savage <[email protected]>
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 used to come with Python 2.7 pre-installed between versions
2110.8 and `12.3 <https://developer.apple.com/documentation/macos-release-notes/macos-12_3-release-notes#Python>`_.
22You are invited to install the most recent version of Python 3 from the Python
23website (https://www.python.org).  A current "universal binary" build of Python,
24which runs natively on the Mac's new Intel and legacy PPC CPU's, is available
25there.
26
27What you get after installing is a number of things:
28
29* A :file:`Python 3.12` folder in your :file:`Applications` folder. In here
30  you find IDLE, the development environment that is a standard part of official
31  Python distributions; and PythonLauncher, which handles double-clicking Python
32  scripts from the Finder.
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
42Apple-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.
46
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 macOS 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. macOS 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
67http://www.barebones.com/products/bbedit/index.html) are good choices, as is
68:program:`TextMate` (see https://macromates.com/). Other editors include
69:program:`Gvim` (https://macvim.org/macvim/) and :program:`Aquamacs`
70(http://aquamacs.org/).
71
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 macOS 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
96With Python 3.9, you can use either :program:`python` or :program:`pythonw`.
97
98
99Configuration
100-------------
101
102Python on macOS 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
106:file:`~/.MacOSX/environment.plist`. See Apple's Technical Document QA1067 for
107details.
108
109For more information on installation Python packages in MacPython, see section
110:ref:`mac-package-manager`.
111
112
113.. _ide:
114
115The IDE
116=======
117
118MacPython ships with the standard IDLE development environment. A good
119introduction to using IDLE can be found at
120http://www.hashcollision.org/hkn/python/idle_intro/index.html.
121
122
123.. _mac-package-manager:
124
125Installing Additional Python Packages
126=====================================
127
128There are several methods to install additional Python packages:
129
130* Packages can be installed via the standard Python distutils mode (``python
131  setup.py install``).
132
133* Many packages can also be installed via the :program:`setuptools` extension
134  or :program:`pip` wrapper, see https://pip.pypa.io/.
135
136
137GUI Programming on the Mac
138==========================
139
140There are several options for building GUI applications on the Mac with Python.
141
142*PyObjC* is a Python binding to Apple's Objective-C/Cocoa framework, which is
143the foundation of most modern Mac development. Information on PyObjC is
144available from https://pypi.org/project/pyobjc/.
145
146The standard Python GUI toolkit is :mod:`tkinter`, based on the cross-platform
147Tk toolkit (https://www.tcl.tk). An Aqua-native version of Tk is bundled with OS
148X by Apple, and the latest version can be downloaded and installed from
149https://www.activestate.com; it can also be built from source.
150
151*wxPython* is another popular cross-platform GUI toolkit that runs natively on
152macOS. Packages and documentation are available from https://www.wxpython.org.
153
154*PyQt* is another popular cross-platform GUI toolkit that runs natively on
155macOS. More information can be found at
156https://riverbankcomputing.com/software/pyqt/intro.
157
158
159Distributing Python Applications on the Mac
160===========================================
161
162The standard tool for deploying standalone Python applications on the Mac is
163:program:`py2app`. More information on installing and using py2app can be found
164at https://pypi.org/project/py2app/.
165
166
167Other Resources
168===============
169
170The MacPython mailing list is an excellent support resource for Python users and
171developers on the Mac:
172
173https://www.python.org/community/sigs/current/pythonmac-sig/
174
175Another useful resource is the MacPython wiki:
176
177https://wiki.python.org/moin/MacPython
178
179