xref: /third_party/python/Doc/using/mac.rst (revision 7db96d56)
17db96d56Sopenharmony_ci
27db96d56Sopenharmony_ci.. _using-on-mac:
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci*********************
57db96d56Sopenharmony_ciUsing Python on a Mac
67db96d56Sopenharmony_ci*********************
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci:Author: Bob Savage <bobsavage@mac.com>
97db96d56Sopenharmony_ci
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ciPython on a Mac running macOS is in principle very similar to Python on
127db96d56Sopenharmony_ciany other Unix platform, but there are a number of additional features such as
137db96d56Sopenharmony_cithe IDE and the Package Manager that are worth pointing out.
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ci.. _getting-osx:
167db96d56Sopenharmony_ci
177db96d56Sopenharmony_ciGetting and Installing MacPython
187db96d56Sopenharmony_ci================================
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_cimacOS used to come with Python 2.7 pre-installed between versions
217db96d56Sopenharmony_ci10.8 and `12.3 <https://developer.apple.com/documentation/macos-release-notes/macos-12_3-release-notes#Python>`_.
227db96d56Sopenharmony_ciYou are invited to install the most recent version of Python 3 from the Python
237db96d56Sopenharmony_ciwebsite (https://www.python.org).  A current "universal binary" build of Python,
247db96d56Sopenharmony_ciwhich runs natively on the Mac's new Intel and legacy PPC CPU's, is available
257db96d56Sopenharmony_cithere.
267db96d56Sopenharmony_ci
277db96d56Sopenharmony_ciWhat you get after installing is a number of things:
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci* A :file:`Python 3.12` folder in your :file:`Applications` folder. In here
307db96d56Sopenharmony_ci  you find IDLE, the development environment that is a standard part of official
317db96d56Sopenharmony_ci  Python distributions; and PythonLauncher, which handles double-clicking Python
327db96d56Sopenharmony_ci  scripts from the Finder.
337db96d56Sopenharmony_ci
347db96d56Sopenharmony_ci* A framework :file:`/Library/Frameworks/Python.framework`, which includes the
357db96d56Sopenharmony_ci  Python executable and libraries. The installer adds this location to your shell
367db96d56Sopenharmony_ci  path. To uninstall MacPython, you can simply remove these three things. A
377db96d56Sopenharmony_ci  symlink to the Python executable is placed in /usr/local/bin/.
387db96d56Sopenharmony_ci
397db96d56Sopenharmony_ciThe Apple-provided build of Python is installed in
407db96d56Sopenharmony_ci:file:`/System/Library/Frameworks/Python.framework` and :file:`/usr/bin/python`,
417db96d56Sopenharmony_cirespectively. You should never modify or delete these, as they are
427db96d56Sopenharmony_ciApple-controlled and are used by Apple- or third-party software.  Remember that
437db96d56Sopenharmony_ciif you choose to install a newer Python version from python.org, you will have
447db96d56Sopenharmony_citwo different but functional Python installations on your computer, so it will
457db96d56Sopenharmony_cibe important that your paths and usages are consistent with what you want to do.
467db96d56Sopenharmony_ci
477db96d56Sopenharmony_ciIDLE includes a help menu that allows you to access Python documentation. If you
487db96d56Sopenharmony_ciare completely new to Python you should start reading the tutorial introduction
497db96d56Sopenharmony_ciin that document.
507db96d56Sopenharmony_ci
517db96d56Sopenharmony_ciIf you are familiar with Python on other Unix platforms you should read the
527db96d56Sopenharmony_cisection on running Python scripts from the Unix shell.
537db96d56Sopenharmony_ci
547db96d56Sopenharmony_ci
557db96d56Sopenharmony_ciHow to run a Python script
567db96d56Sopenharmony_ci--------------------------
577db96d56Sopenharmony_ci
587db96d56Sopenharmony_ciYour best way to get started with Python on macOS is through the IDLE
597db96d56Sopenharmony_ciintegrated development environment, see section :ref:`ide` and use the Help menu
607db96d56Sopenharmony_ciwhen the IDE is running.
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ciIf you want to run Python scripts from the Terminal window command line or from
637db96d56Sopenharmony_cithe Finder you first need an editor to create your script. macOS comes with a
647db96d56Sopenharmony_cinumber of standard Unix command line editors, :program:`vim` and
657db96d56Sopenharmony_ci:program:`emacs` among them. If you want a more Mac-like editor,
667db96d56Sopenharmony_ci:program:`BBEdit` or :program:`TextWrangler` from Bare Bones Software (see
677db96d56Sopenharmony_cihttp://www.barebones.com/products/bbedit/index.html) are good choices, as is
687db96d56Sopenharmony_ci:program:`TextMate` (see https://macromates.com/). Other editors include
697db96d56Sopenharmony_ci:program:`Gvim` (https://macvim.org/macvim/) and :program:`Aquamacs`
707db96d56Sopenharmony_ci(http://aquamacs.org/).
717db96d56Sopenharmony_ci
727db96d56Sopenharmony_ciTo run your script from the Terminal window you must make sure that
737db96d56Sopenharmony_ci:file:`/usr/local/bin` is in your shell search path.
747db96d56Sopenharmony_ci
757db96d56Sopenharmony_ciTo run your script from the Finder you have two options:
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ci* Drag it to :program:`PythonLauncher`
787db96d56Sopenharmony_ci
797db96d56Sopenharmony_ci* Select :program:`PythonLauncher` as the default application to open your
807db96d56Sopenharmony_ci  script (or any .py script) through the finder Info window and double-click it.
817db96d56Sopenharmony_ci  :program:`PythonLauncher` has various preferences to control how your script is
827db96d56Sopenharmony_ci  launched. Option-dragging allows you to change these for one invocation, or use
837db96d56Sopenharmony_ci  its Preferences menu to change things globally.
847db96d56Sopenharmony_ci
857db96d56Sopenharmony_ci
867db96d56Sopenharmony_ci.. _osx-gui-scripts:
877db96d56Sopenharmony_ci
887db96d56Sopenharmony_ciRunning scripts with a GUI
897db96d56Sopenharmony_ci--------------------------
907db96d56Sopenharmony_ci
917db96d56Sopenharmony_ciWith older versions of Python, there is one macOS quirk that you need to be
927db96d56Sopenharmony_ciaware of: programs that talk to the Aqua window manager (in other words,
937db96d56Sopenharmony_cianything that has a GUI) need to be run in a special way. Use :program:`pythonw`
947db96d56Sopenharmony_ciinstead of :program:`python` to start such scripts.
957db96d56Sopenharmony_ci
967db96d56Sopenharmony_ciWith Python 3.9, you can use either :program:`python` or :program:`pythonw`.
977db96d56Sopenharmony_ci
987db96d56Sopenharmony_ci
997db96d56Sopenharmony_ciConfiguration
1007db96d56Sopenharmony_ci-------------
1017db96d56Sopenharmony_ci
1027db96d56Sopenharmony_ciPython on macOS honors all standard Unix environment variables such as
1037db96d56Sopenharmony_ci:envvar:`PYTHONPATH`, but setting these variables for programs started from the
1047db96d56Sopenharmony_ciFinder is non-standard as the Finder does not read your :file:`.profile` or
1057db96d56Sopenharmony_ci:file:`.cshrc` at startup. You need to create a file
1067db96d56Sopenharmony_ci:file:`~/.MacOSX/environment.plist`. See Apple's Technical Document QA1067 for
1077db96d56Sopenharmony_cidetails.
1087db96d56Sopenharmony_ci
1097db96d56Sopenharmony_ciFor more information on installation Python packages in MacPython, see section
1107db96d56Sopenharmony_ci:ref:`mac-package-manager`.
1117db96d56Sopenharmony_ci
1127db96d56Sopenharmony_ci
1137db96d56Sopenharmony_ci.. _ide:
1147db96d56Sopenharmony_ci
1157db96d56Sopenharmony_ciThe IDE
1167db96d56Sopenharmony_ci=======
1177db96d56Sopenharmony_ci
1187db96d56Sopenharmony_ciMacPython ships with the standard IDLE development environment. A good
1197db96d56Sopenharmony_ciintroduction to using IDLE can be found at
1207db96d56Sopenharmony_cihttp://www.hashcollision.org/hkn/python/idle_intro/index.html.
1217db96d56Sopenharmony_ci
1227db96d56Sopenharmony_ci
1237db96d56Sopenharmony_ci.. _mac-package-manager:
1247db96d56Sopenharmony_ci
1257db96d56Sopenharmony_ciInstalling Additional Python Packages
1267db96d56Sopenharmony_ci=====================================
1277db96d56Sopenharmony_ci
1287db96d56Sopenharmony_ciThere are several methods to install additional Python packages:
1297db96d56Sopenharmony_ci
1307db96d56Sopenharmony_ci* Packages can be installed via the standard Python distutils mode (``python
1317db96d56Sopenharmony_ci  setup.py install``).
1327db96d56Sopenharmony_ci
1337db96d56Sopenharmony_ci* Many packages can also be installed via the :program:`setuptools` extension
1347db96d56Sopenharmony_ci  or :program:`pip` wrapper, see https://pip.pypa.io/.
1357db96d56Sopenharmony_ci
1367db96d56Sopenharmony_ci
1377db96d56Sopenharmony_ciGUI Programming on the Mac
1387db96d56Sopenharmony_ci==========================
1397db96d56Sopenharmony_ci
1407db96d56Sopenharmony_ciThere are several options for building GUI applications on the Mac with Python.
1417db96d56Sopenharmony_ci
1427db96d56Sopenharmony_ci*PyObjC* is a Python binding to Apple's Objective-C/Cocoa framework, which is
1437db96d56Sopenharmony_cithe foundation of most modern Mac development. Information on PyObjC is
1447db96d56Sopenharmony_ciavailable from https://pypi.org/project/pyobjc/.
1457db96d56Sopenharmony_ci
1467db96d56Sopenharmony_ciThe standard Python GUI toolkit is :mod:`tkinter`, based on the cross-platform
1477db96d56Sopenharmony_ciTk toolkit (https://www.tcl.tk). An Aqua-native version of Tk is bundled with OS
1487db96d56Sopenharmony_ciX by Apple, and the latest version can be downloaded and installed from
1497db96d56Sopenharmony_cihttps://www.activestate.com; it can also be built from source.
1507db96d56Sopenharmony_ci
1517db96d56Sopenharmony_ci*wxPython* is another popular cross-platform GUI toolkit that runs natively on
1527db96d56Sopenharmony_cimacOS. Packages and documentation are available from https://www.wxpython.org.
1537db96d56Sopenharmony_ci
1547db96d56Sopenharmony_ci*PyQt* is another popular cross-platform GUI toolkit that runs natively on
1557db96d56Sopenharmony_cimacOS. More information can be found at
1567db96d56Sopenharmony_cihttps://riverbankcomputing.com/software/pyqt/intro.
1577db96d56Sopenharmony_ci
1587db96d56Sopenharmony_ci
1597db96d56Sopenharmony_ciDistributing Python Applications on the Mac
1607db96d56Sopenharmony_ci===========================================
1617db96d56Sopenharmony_ci
1627db96d56Sopenharmony_ciThe standard tool for deploying standalone Python applications on the Mac is
1637db96d56Sopenharmony_ci:program:`py2app`. More information on installing and using py2app can be found
1647db96d56Sopenharmony_ciat https://pypi.org/project/py2app/.
1657db96d56Sopenharmony_ci
1667db96d56Sopenharmony_ci
1677db96d56Sopenharmony_ciOther Resources
1687db96d56Sopenharmony_ci===============
1697db96d56Sopenharmony_ci
1707db96d56Sopenharmony_ciThe MacPython mailing list is an excellent support resource for Python users and
1717db96d56Sopenharmony_cidevelopers on the Mac:
1727db96d56Sopenharmony_ci
1737db96d56Sopenharmony_cihttps://www.python.org/community/sigs/current/pythonmac-sig/
1747db96d56Sopenharmony_ci
1757db96d56Sopenharmony_ciAnother useful resource is the MacPython wiki:
1767db96d56Sopenharmony_ci
1777db96d56Sopenharmony_cihttps://wiki.python.org/moin/MacPython
1787db96d56Sopenharmony_ci
179