• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2.. _using-on-mac:
3
4*********************
5Using Python on macOS
6*********************
7
8.. sectionauthor:: Bob Savage <bobsavage@mac.com>
9.. sectionauthor:: Ned Deily <nad@python.org>
10
11This document aims to give an overview of macOS-specific behavior you should
12know about to get started with Python on Mac computers.
13Python on a Mac running macOS is very similar to Python on other Unix-derived platforms,
14but there are some differences in installation and some features.
15
16There are various ways to obtain and install Python for macOS.
17Pre-built versions of the most recent versions of Python are available
18from a number of distributors. Much of this document describes use of
19the Pythons provided by the CPython release team for download from
20the `python.org website <https://www.python.org/downloads/>`_. See
21:ref:`alternative_bundles` for some other options.
22
23.. |usemac_x_dot_y| replace:: 3.13
24.. |usemac_python_x_dot_y_literal| replace:: ``python3.13``
25.. |usemac_python_x_dot_y_t_literal| replace:: ``python3.13t``
26.. |usemac_python_x_dot_y_t_literal_config| replace:: ``python3.13t-config``
27.. |usemac_applications_folder_name| replace:: ``Python 3.13``
28.. |usemac_applications_folder_version| replace:: ``/Applications/Python 3.13/``
29
30.. _getting-osx:
31.. _getting-and-installing-macpython:
32
33Using Python for macOS from ``python.org``
34==========================================
35
36Installation steps
37------------------
38
39For `current Python versions <https://www.python.org/downloads/>`_
40(other than those in ``security`` status), the release team produces a
41**Python for macOS** installer package for each new release.
42A list of available installers
43is available `here <https://www.python.org/downloads/macos/>`_.
44We recommend using the most recent supported Python version where possible.
45Current installers provide a
46`universal2 binary <https://en.wikipedia.org/wiki/Universal_binary>`_ build
47of Python which runs natively on all Macs (Apple Silicon and Intel) that are
48supported by a wide range of macOS versions,
49currently typically from at least **macOS 10.13 High Sierra** on.
50
51The downloaded file is a standard macOS installer package file (``.pkg``).
52File integrity information (checksum, size, sigstore signature, etc) for each file is included
53on the release download page. Installer packages and their contents are signed and notarized
54with ``Python Software Foundation`` Apple Developer ID certificates
55to meet `macOS Gatekeeper requirements <https://support.apple.com/en-us/102445>`_.
56
57For a default installation, double-click on the downloaded installer package file.
58This should launch the standard macOS Installer app and display the first of several
59installer windows steps.
60
61.. image:: mac_installer_01_introduction.png
62
63Clicking on the **Continue** button brings up the **Read Me** for this installer.
64Besides other important information, the **Read Me** documents which Python version is
65going to be installed and on what versions of macOS it is supported. You may need
66to scroll through to read the whole file. By default, this **Read Me** will also be
67installed in |usemac_applications_folder_version| and available to read anytime.
68
69.. image:: mac_installer_02_readme.png
70
71Clicking on **Continue** proceeds to display the license for Python and for
72other included software. You will then need to **Agree** to the license terms
73before proceeding to the next step. This license file will also be installed
74and available to be read later.
75
76.. image:: mac_installer_03_license.png
77
78After the license terms are accepted, the next step is the **Installation Type**
79display. For most uses, the standard set of installation operations is appropriate.
80
81.. image:: mac_installer_04_installation_type.png
82
83By pressing the **Customize** button, you can choose to omit or select certain package
84components of the installer. Click on each package name to see a description of
85what it installs.
86To also install support for the optional experimental free-threaded feature,
87see :ref:`install-freethreaded-macos`.
88
89.. image:: mac_installer_05_custom_install.png
90
91In either case, clicking **Install** will begin the install process by asking
92permission to install new software. A macOS user name with ``Administrator`` privilege
93is needed as the installed Python will be available to all users of the Mac.
94
95When the installation is complete, the **Summary** window will appear.
96
97.. image:: mac_installer_06_summary.png
98
99Double-click on the :command:`Install Certificates.command`
100icon or file in the |usemac_applications_folder_version| window to complete the
101installation.
102
103.. image:: mac_installer_07_applications.png
104
105This will open a temporary :program:`Terminal` shell window that
106will use the new Python to download and install SSL root certificates
107for its use.
108
109.. image:: mac_installer_08_install_certificates.png
110
111If ``Successfully installed certifi`` and ``update complete`` appears
112in the terminal window, the installation is complete.
113Close this terminal window and the installer window.
114
115A default install will include:
116
117* A |usemac_applications_folder_name| folder in your :file:`Applications` folder. In here
118  you find :program:`IDLE`, the development environment that is a standard part of official
119  Python distributions; and :program:`Python Launcher`, which handles double-clicking Python
120  scripts from the macOS `Finder <https://support.apple.com/en-us/HT201732>`_.
121
122* A framework :file:`/Library/Frameworks/Python.framework`, which includes the
123  Python executable and libraries. The installer adds this location to your shell
124  path. To uninstall Python, you can remove these three things.
125  Symlinks to the Python executable are placed in :file:`/usr/local/bin/`.
126
127.. note::
128
129   Recent versions of macOS include a :command:`python3` command in :file:`/usr/bin/python3`
130   that links to a usually older and incomplete version of Python provided by and for use by
131   the Apple development tools, :program:`Xcode` or the :program:`Command Line Tools for Xcode`.
132   You should never modify or attempt to delete this installation, as it is
133   Apple-controlled and is used by Apple-provided or third-party software.  If
134   you choose to install a newer Python version from ``python.org``, you will have
135   two different but functional Python installations on your computer that
136   can co-exist. The default installer options should ensure that its :command:`python3`
137   will be used instead of the system :command:`python3`.
138
139How to run a Python script
140--------------------------
141
142There are two ways to invoke the Python interpreter.
143If you are familiar with using a Unix shell in a terminal
144window, you can invoke |usemac_python_x_dot_y_literal| or ``python3`` optionally
145followed by one or more command line options (described in :ref:`using-on-general`).
146The Python tutorial also has a useful section on
147:ref:`using Python interactively from a shell <tut-interac>`.
148
149You can also invoke the interpreter through an integrated
150development environment.
151:ref:`idle` is a basic editor and interpreter environment
152which is included with the standard distribution of Python.
153:program:`IDLE` includes a Help menu that allows you to access Python documentation. If you
154are completely new to Python, you can read the tutorial introduction
155in that document.
156
157There are many other editors and IDEs available, see :ref:`editors`
158for more information.
159
160To run a Python script file from the terminal window, you can
161invoke the interpreter with the name of the script file:
162
163    |usemac_python_x_dot_y_literal| ``myscript.py``
164
165To run your script from the Finder, you can either:
166
167* Drag it to :program:`Python Launcher`.
168
169* Select :program:`Python Launcher` as the default application to open your
170  script (or any ``.py`` script) through the Finder Info window and double-click it.
171  :program:`Python Launcher` has various preferences to control how your script is
172  launched. Option-dragging allows you to change these for one invocation, or use
173  its ``Preferences`` menu to change things globally.
174
175Be aware that running the script directly from the macOS Finder might
176produce different results than when running from a terminal window as
177the script will not be run in the usual shell environment including
178any setting of environment variables in shell profiles.
179And, as with any other script or program,
180be certain of what you are about to run.
181
182.. _alternative_bundles:
183
184Alternative Distributions
185=========================
186
187Besides the standard ``python.org`` for macOS installer, there are third-party
188distributions for macOS that may include additional functionality.
189Some popular distributions and their key features:
190
191`ActivePython <https://www.activestate.com/products/python/>`_
192    Installer with multi-platform compatibility, documentation
193
194`Anaconda <https://www.anaconda.com/download/>`_
195    Popular scientific modules (such as numpy, scipy, and pandas) and the
196    ``conda`` package manager.
197
198`Homebrew <https://brew.sh>`_
199    Package manager for macOS including multiple versions of Python and many
200    third-party Python-based packages (including numpy, scipy, and pandas).
201
202`MacPorts <https://www.macports.org>`_
203    Another package manager for macOS including multiple versions of Python and many
204    third-party Python-based packages. May include pre-built versions of Python and
205    many packages for older versions of macOS.
206
207Note that distributions might not include the latest versions of Python or
208other libraries, and are not maintained or supported by the core Python team.
209
210.. _mac-package-manager:
211
212Installing Additional Python Packages
213=====================================
214
215Refer to the `Python Packaging User Guide`_ for more information.
216
217.. _Python Packaging User Guide: https://packaging.python.org/en/latest/tutorials/installing-packages/
218
219
220.. _osx-gui-scripts:
221
222.. _gui-programming-on-the-mac:
223
224GUI Programming
225===============
226
227There are several options for building GUI applications on the Mac with Python.
228
229The standard Python GUI toolkit is :mod:`tkinter`, based on the cross-platform
230Tk toolkit (https://www.tcl.tk). A macOS-native version of Tk is included with
231the installer.
232
233*PyObjC* is a Python binding to Apple's Objective-C/Cocoa framework.
234Information on PyObjC is available from :pypi:`pyobjc`.
235
236A number of alternative macOS GUI toolkits are available including:
237
238* `PySide <https://www.qt.io/qt-for-python>`_: Official Python bindings to the
239  `Qt GUI toolkit <https://wiki.qt.io/Qt_for_Python>`_.
240
241* `PyQt <https://riverbankcomputing.com/software/pyqt/>`_: Alternative
242  Python bindings to Qt.
243
244* `Kivy <https://kivy.org>`_: A cross-platform GUI toolkit that supports
245  desktop and mobile platforms.
246
247* `Toga <https://toga.readthedocs.io>`_: Part of the `BeeWare Project
248  <https://beeware.org>`_; supports desktop, mobile, web and console apps.
249
250* `wxPython <https://wxpython.org>`_: A cross-platform toolkit that
251  supports desktop operating systems.
252
253
254Advanced Topics
255===============
256
257.. _install-freethreaded-macos:
258
259Installing Free-threaded Binaries
260---------------------------------
261
262.. versionadded:: 3.13 (Experimental)
263
264.. note::
265
266   Everything described in this section is considered experimental,
267   and should be expected to change in future releases.
268
269The ``python.org`` :ref:`Python for macOS <getting-and-installing-macpython>`
270installer package can optionally install an additional build of
271Python |usemac_x_dot_y| that supports :pep:`703`, the experimental free-threading feature
272(running with the :term:`global interpreter lock` disabled).
273Check the release page on ``python.org`` for possible updated information.
274
275Because this feature is still considered experimental, the support for it
276is not installed by default. It is packaged as a separate install option,
277available by clicking the **Customize** button on the **Installation Type**
278step of the installer as described above.
279
280.. image:: mac_installer_09_custom_install_free_threaded.png
281
282If the box next to the **Free-threaded Python** package name is checked,
283a separate :file:`PythonT.framework` will also be installed
284alongside the normal :file:`Python.framework` in :file:`/Library/Frameworks`.
285This configuration allows a free-threaded Python |usemac_x_dot_y| build to co-exist
286on your system with a traditional (GIL only) Python |usemac_x_dot_y| build with
287minimal risk while installing or testing. This installation layout is itself
288experimental and is subject to change in future releases.
289
290Known cautions and limitations:
291
292- The **UNIX command-line tools** package, which is selected by default,
293  will install links in :file:`/usr/local/bin` for |usemac_python_x_dot_y_t_literal|,
294  the free-threaded interpreter, and |usemac_python_x_dot_y_t_literal_config|,
295  a configuration utility which may be useful for package builders.
296  Since :file:`/usr/local/bin` is typically included in your shell ``PATH``,
297  in most cases no changes to your ``PATH`` environment variables should
298  be needed to use |usemac_python_x_dot_y_t_literal|.
299
300- For this release, the **Shell profile updater** package and the
301  :file:`Update Shell Profile.command` in |usemac_applications_folder_version|
302  do not support the free-threaded package.
303
304- The free-threaded build and the traditional build have separate search
305  paths and separate :file:`site-packages` directories so, by default,
306  if you need a package available in both builds, it may need to be installed in both.
307  The free-threaded package will install a separate instance of :program:`pip` for use
308  with |usemac_python_x_dot_y_t_literal|.
309
310  - To install a package using :command:`pip` without a :command:`venv`:
311
312        |usemac_python_x_dot_y_t_literal| ``-m pip install <package_name>``
313
314- When working with multiple Python environments, it is usually safest and easiest
315  to :ref:`create and use virtual environments <tut-venv>`.
316  This can avoid possible command name conflicts and confusion about which Python is in use:
317
318      |usemac_python_x_dot_y_t_literal| ``-m venv <venv_name>``
319
320  then :command:`activate`.
321
322- To run a free-threaded version of IDLE:
323
324      |usemac_python_x_dot_y_t_literal| ``-m idlelib``
325
326- The interpreters in both builds respond to the same
327  :ref:`PYTHON environment variables <using-on-envvars>`
328  which may have unexpected results, for example, if you have ``PYTHONPATH``
329  set in a shell profile. If necessary, there are
330  :ref:`command line options <using-on-interface-options>` like ``-E``
331  to ignore these environment variables.
332
333- The free-threaded build links to the third-party shared libraries,
334  such as ``OpenSSL`` and ``Tk``, installed in the traditional framework.
335  This means that both builds also share one set of trust certificates
336  as installed by the :command:`Install Certificates.command` script,
337  thus it only needs to be run once.
338
339- If you cannot depend on the link in ``/usr/local/bin`` pointing to the
340  ``python.org`` free-threaded |usemac_python_x_dot_y_t_literal| (for example, if you want
341  to install your own version there or some other distribution does),
342  you can explicitly set your shell ``PATH`` environment variable to
343  include the ``PythonT`` framework ``bin`` directory:
344
345  .. code-block:: sh
346
347     export PATH="/Library/Frameworks/PythonT.framework/Versions/3.13/bin":"$PATH"
348
349  The traditional framework installation by default does something similar,
350  except for :file:`Python.framework`. Be aware that having both framework ``bin``
351  directories in ``PATH`` can lead to confusion if there are duplicate names
352  like ``python3.13`` in both; which one is actually used depends on the order
353  they appear in ``PATH``. The ``which python3.x`` or ``which python3.xt``
354  commands can show which path is being used. Using virtual environments
355  can help avoid such ambiguities. Another option might be to create
356  a shell :command:`alias` to the desired interpreter, like:
357
358  .. code-block:: sh
359
360     alias py3.13="/Library/Frameworks/Python.framework/Versions/3.13/bin/python3.13"
361     alias py3.13t="/Library/Frameworks/PythonT.framework/Versions/3.13/bin/python3.13t"
362
363Installing using the command line
364---------------------------------
365
366If you want to use automation to install the ``python.org`` installer package
367(rather than by using the familiar macOS :program:`Installer` GUI app),
368the macOS command line :command:`installer` utility lets you select non-default
369options, too. If you are not familiar with :command:`installer`, it can be
370somewhat cryptic (see :command:`man installer` for more information).
371As an example, the following shell snippet shows one way to do it,
372using the ``3.13.0b2`` release and selecting the free-threaded interpreter
373option:
374
375..  code-block:: sh
376
377    RELEASE="python-3.13.0b2-macos11.pkg"
378
379    # download installer pkg
380    curl -O https://www.python.org/ftp/python/3.13.0/${RELEASE}
381
382    # create installer choicechanges to customize the install:
383    #    enable the PythonTFramework-3.13 package
384    #    while accepting the other defaults (install all other packages)
385    cat > ./choicechanges.plist <<EOF
386    <?xml version="1.0" encoding="UTF-8"?>
387    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
388    <plist version="1.0">
389    <array>
390            <dict>
391                    <key>attributeSetting</key>
392                    <integer>1</integer>
393                    <key>choiceAttribute</key>
394                    <string>selected</string>
395                    <key>choiceIdentifier</key>
396                    <string>org.python.Python.PythonTFramework-3.13</string>
397            </dict>
398    </array>
399    </plist>
400    EOF
401
402    sudo installer -pkg ./${RELEASE} -applyChoiceChangesXML ./choicechanges.plist -target /
403
404
405You can then test that both installer builds are now available with something like:
406
407..  code-block:: console
408
409    $ # test that the free-threaded interpreter was installed if the Unix Command Tools package was enabled
410    $ /usr/local/bin/python3.13t -VV
411    Python 3.13.0b2 experimental free-threading build (v3.13.0b2:3a83b172af, Jun  5 2024, 12:57:31) [Clang 15.0.0 (clang-1500.3.9.4)]
412    $ #    and the traditional interpreter
413    $ /usr/local/bin/python3.13 -VV
414    Python 3.13.0b2 (v3.13.0b2:3a83b172af, Jun  5 2024, 12:50:24) [Clang 15.0.0 (clang-1500.3.9.4)]
415    $ # test that they are also available without the prefix if /usr/local/bin is on $PATH
416    $ python3.13t -VV
417    Python 3.13.0b2 experimental free-threading build (v3.13.0b2:3a83b172af, Jun  5 2024, 12:57:31) [Clang 15.0.0 (clang-1500.3.9.4)]
418    $ python3.13 -VV
419    Python 3.13.0b2 (v3.13.0b2:3a83b172af, Jun  5 2024, 12:50:24) [Clang 15.0.0 (clang-1500.3.9.4)]
420
421.. note::
422
423   Current ``python.org`` installers only install to fixed locations like
424   :file:`/Library/Frameworks/`, :file:`/Applications`, and :file:`/usr/local/bin`.
425   You cannot use the :command:`installer` ``-domain`` option to install to
426   other locations.
427
428.. _distributing-python-applications-on-the-mac:
429
430Distributing Python Applications
431--------------------------------
432
433A range of tools exist for converting your Python code into a standalone
434distributable application:
435
436* :pypi:`py2app`: Supports creating macOS ``.app``
437  bundles from a Python project.
438
439* `Briefcase <https://briefcase.readthedocs.io>`_: Part of the `BeeWare Project
440  <https://beeware.org>`_; a cross-platform packaging tool that supports
441  creation of ``.app`` bundles on macOS, as well as managing signing and
442  notarization.
443
444* `PyInstaller <https://pyinstaller.org/>`_: A cross-platform packaging tool that creates
445  a single file or folder as a distributable artifact.
446
447App Store Compliance
448--------------------
449
450Apps submitted for distribution through the macOS App Store must pass Apple's
451app review process. This process includes a set of automated validation rules
452that inspect the submitted application bundle for problematic code.
453
454The Python standard library contains some code that is known to violate these
455automated rules. While these violations appear to be false positives, Apple's
456review rules cannot be challenged. Therefore, it is necessary to modify the
457Python standard library for an app to pass App Store review.
458
459The Python source tree contains
460:source:`a patch file <Mac/Resources/app-store-compliance.patch>` that will remove
461all code that is known to cause issues with the App Store review process. This
462patch is applied automatically when CPython is configured with the
463:option:`--with-app-store-compliance` option.
464
465This patch is not normally required to use CPython on a Mac; nor is it required
466if you are distributing an app *outside* the macOS App Store. It is *only*
467required if you are using the macOS App Store as a distribution channel.
468
469Other Resources
470===============
471
472The `python.org Help page <https://www.python.org/about/help/>`_ has links to many useful resources.
473The `Pythonmac-SIG mailing list <https://www.python.org/community/sigs/current/pythonmac-sig/>`_
474is another support resource specifically for Python users and developers on the Mac.
475