1====================== 2Python on macOS README 3====================== 4 5:Authors: 6 Jack Jansen (2004-07), 7 Ronald Oussoren (2010-04), 8 Ned Deily (2012-06) 9 10This document provides a quick overview of some macOS specific features in 11the Python distribution. 12 13Compilers for building on macOS 14=============================== 15 16The core developers primarily test builds on macOS with Apple's compiler tools, 17either Xcode or the Command Line Tools. For these we only support building with 18a compiler that includes an SDK that targets the OS on the build machine, that is 19the version of Xcode that shipped with the OS version or one newer. 20 21For example, for macOS 12 we support Xcode 13 and Xcode 14 (or the corresponding 22Command Line Tools). 23 24Building with other compilers, such as GCC, likely works, but is not actively supported. 25 26macOS specific arguments to configure 27===================================== 28 29* ``--enable-framework[=DIR]`` 30 31 If this argument is specified the build will create a Python.framework rather 32 than a traditional Unix install. See the section 33 _`Building and using a framework-based Python on macOS` for more 34 information on frameworks. 35 36 If the optional directory argument is specified the framework is installed 37 into that directory. This can be used to install a python framework into 38 your home directory:: 39 40 $ ./configure --enable-framework=/Users/ronald/Library/Frameworks 41 $ make && make install 42 43 This will install the framework itself in ``/Users/ronald/Library/Frameworks``, 44 the applications in a subdirectory of ``/Users/ronald/Applications`` and the 45 command-line tools in ``/Users/ronald/bin``. 46 47* ``--with-framework-name=NAME`` 48 49 Specify the name for the python framework, defaults to ``Python``. This option 50 is only valid when ``--enable-framework`` is specified. 51 52* ``--enable-universalsdk[=PATH]`` 53 54 Create a universal binary build of Python. This can be used with both 55 regular and framework builds. 56 57 The optional argument specifies which macOS SDK should be used to perform the 58 build. In most cases on current systems, you do not need to specify PATH or 59 you can just use ``/``; the default MacOSX SDK for the active Xcode or Command 60 Line Tools developer directory will be used. See the macOS ``xcrun`` man page 61 for more information. Current versions of macOS and Xcode no longer install 62 system header files in their traditional locations, like ``/usr/include`` and 63 ``/System/Library/Frameworks``; instead they are found within a MacOSX SDK. 64 The Apple-supplied build tools handle this transparently and current 65 versions of Python now handle this as well. So it is no longer necessary, 66 and since macOS 10.14, no longer possible to force the installation of system 67 headers with ``xcode-select``. 68 69* ``--with-universal-archs=VALUE`` 70 71 Specify the kind of universal binary that should be created. This option is 72 only valid when ``--enable-universalsdk`` is specified. The default is 73 ``32-bit`` if building with a SDK that supports PPC, otherwise defaults 74 to ``intel``. Note that ``intel`` means a universal build of both 32-bit 75 and 64-bit binaries and that may not be what you want; for example, 76 as of macOS 10.15 Catalina, 32-bit execution is no longer supported by 77 the operating system. Thus it is best to either explicitly specify 78 values for ``--with-universal-archs``: 79 80 ``--enable-universalsdk --with-universal-archs=intel-64`` 81 82 or avoid using either. 83 84 85Building and using a universal binary of Python on macOS 86======================================================== 87 881. What is a universal binary 89----------------------------- 90 91A universal binary build of Python contains object code for more than one 92CPU architecture. A universal macOS executable file or library combines the 93architecture-specific code into one file and can therefore run at native 94speed on all supported architectures. Universal files were introduced in 95macOS 10.4 to add support for Intel-based Macs to the existing PowerPC (PPC) 96machines. In macOS 10.5 support was extended to 64-bit Intel and 64-bit PPC 97architectures. It is possible to build Python with various combinations 98of architectures depending on the build tools and macOS version in use. 99Note that PPC support was removed in macOS 10.7 and 32-bit Intel support 100was removed in macOS 10.15. So currently as of macOS 10.15, macOS only 101supports one execution architecture, 64-bit Intel (``x86_64``). 102 1032. How do I build a universal binary 104------------------------------------ 105 106You can enable universal binaries by specifying the "--enable-universalsdk" 107flag to configure:: 108 109 $ ./configure --enable-universalsdk 110 $ make 111 $ make install 112 113This flag can be used with a framework build of python, but also with a classic 114unix build. Universal builds were first supported with macOS 10.4 with Xcode 2.1 115and the 10.4u SDK. Starting with Xcode 3 and macOS 10.5, more configurations are 116available. 117 118In general, universal builds depend on specific features provided by the 119Apple-supplied compilers and other build tools included in Apple's Xcode 120development tools. You should install Xcode or the command line tools 121component appropriate for the macOS release you are running on. See the 122Python Developer's Guide (https://devguide.python.org/setup/) 123for more information. 124 1252.1 Flavors of universal binaries 126................................. 127 128It is possible to build a number of flavors of the universal binary build, 129the default is a 32-bit only binary (i386 and ppc) in build environments that 130support ppc (10.4 with Xcode 2, 10.5 and 10.6 with Xcode 3) or an 131Intel-32/-64-bit binary (i386 and X86_64) in build environments that do not 132support ppc (Xcode 4 on 10.6 and later systems). The flavor can be specified 133using the configure option ``--with-universal-archs=VALUE``. The following 134values are available: 135 136 * ``universal2``: ``arm64``, ``x86_64`` 137 138 * ``intel``: ``i386``, ``x86_64`` 139 140 * ``intel-32``: ``i386`` 141 142 * ``intel-64``: ``x86_64`` 143 144 * ``32-bit``: ``ppc``, ``i386`` 145 146 * ``3-way``: ``i386``, ``x86_64``, ``ppc`` 147 148 * ``64-bit``: ``ppc64``, ``x86_64`` 149 150 * ``all``: ``ppc``, ``ppc64``, ``i386``, ``x86_64`` 151 152To build a universal binary that includes a 64-bit architecture, you must build 153on a system running macOS 10.5 or later. The ``all`` and ``64-bit`` flavors can 154only be built with a 10.5 SDK because ``ppc64`` support was only included with 155macOS 10.5. Although legacy ``ppc`` support was included with Xcode 3 on macOS 15610.6, it was removed in Xcode 4, versions of which were released on macOS 10.6 157and which is the standard for macOS 10.7. To summarize, the 158following combinations of SDKs and universal-archs flavors are available: 159 160 * 10.4u SDK with Xcode 2 supports ``32-bit`` only 161 162 * 10.5 SDK with Xcode 3.1.x supports all flavors 163 164 * 10.6 SDK with Xcode 3.2.x supports ``intel``, ``intel-32``, 165 ``intel-64``, ``3-way``, and ``32-bit`` 166 167 * 10.6 SDK with Xcode 4 supports ``intel``, ``intel-32``, and ``intel-64`` 168 169 * 10.7 through 10.14 SDKs support ``intel``, ``intel-32``, and ``intel-64`` 170 171 * 10.15 and later SDKs support ``intel-64`` only 172 173 * 11.0 and later SDKs support ``universal2`` 174 175The makefile for a framework build will also install ``python3.x-32`` 176binaries when the universal architecture includes at least one 32-bit 177architecture (that is, for all flavors but ``64-bit`` and ``intel-64``). 178It will also install ``python3.x-intel64`` binaries in the ``universal2`` 179case to allow easy execution with the Rosetta 2 Intel emulator on Apple 180Silicon Macs. 181 182Running a specific architecture 183............................... 184 185You can run code using a specific architecture using the ``arch`` command:: 186 187 $ arch -i386 python 188 189Or to explicitly run in 32-bit mode, regardless of the machine hardware:: 190 191 $ arch -i386 -ppc python 192 193Using ``arch`` is not a perfect solution as the selected architecture will 194not automatically carry through to subprocesses launched by programs and tests 195under that Python. If you want to ensure that Python interpreters launched in 196subprocesses also run in 32-bit-mode if the main interpreter does, use 197a ``python3.x-32`` binary and use the value of ``sys.executable`` as the 198``subprocess`` ``Popen`` executable value. 199 200Likewise, use ``python3.x-intel64`` to force execution in ``x86_64`` mode 201with ``universal2`` binaries. 202 203Building and using a framework-based Python on macOS 204==================================================== 205 206 2071. Why would I want a framework Python instead of a normal static Python? 208------------------------------------------------------------------------- 209 210The main reason is because you want to create GUI programs in Python. With the 211exception of X11/XDarwin-based GUI toolkits all GUI programs need to be run 212from a macOS application bundle (".app"). 213 214While it is technically possible to create a .app without using frameworks you 215will have to do the work yourself if you really want this. 216 217A second reason for using frameworks is that they put Python-related items in 218only two places: "/Library/Framework/Python.framework" and 219"/Applications/Python <VERSION>" where ``<VERSION>`` can be e.g. "3.8", 220"2.7", etc. This simplifies matters for users installing 221Python from a binary distribution if they want to get rid of it again. Moreover, 222due to the way frameworks work, users without admin privileges can install a 223binary distribution in their home directory without recompilation. 224 2252. How does a framework Python differ from a normal static Python? 226------------------------------------------------------------------ 227 228In everyday use there is no difference, except that things are stored in 229a different place. If you look in /Library/Frameworks/Python.framework 230you will see lots of relative symlinks, see the Apple documentation for 231details. If you are used to a normal unix Python file layout go down to 232Versions/Current and you will see the familiar bin and lib directories. 233 2343. Do I need extra packages? 235---------------------------- 236 237Yes, probably. If you want Tkinter support you need to get the macOS AquaTk 238distribution, this is installed by default on macOS 10.4 or later. Be 239aware, though, that the Cocoa-based AquaTk's supplied starting with macOS 24010.6 have proven to be unstable. If possible, you should consider 241installing a newer version before building on macOS 10.6 or later, such as 242the ActiveTcl 8.6. See https://www.python.org/download/mac/tcltk/. If you 243are building with an SDK, ensure that the newer Tcl and Tk frameworks are 244seen in the SDK's ``Library/Frameworks`` directory; you may need to 245manually create symlinks to their installed location, ``/Library/Frameworks``. 246If you want wxPython you need to get that. 247If you want Cocoa you need to get PyObjC. 248 2494. How do I build a framework Python? 250------------------------------------- 251 252This directory contains a Makefile that will create a couple of python-related 253applications (full-blown macOS .app applications, that is) in 254"/Applications/Python <VERSION>", and a hidden helper application Python.app 255inside the Python.framework, and unix tools including "python" into 256/usr/local/bin. In addition it has a target "installmacsubtree" that installs 257the relevant portions of the Mac subtree into the Python.framework. 258 259It is normally invoked indirectly through the main Makefile, as the last step 260in the sequence 261 262 1. ./configure --enable-framework 263 264 2. make 265 266 3. make install 267 268This sequence will put the framework in ``/Library/Framework/Python.framework``, 269the applications in ``/Applications/Python <VERSION>`` and the unix tools in 270``/usr/local/bin``. 271 272Installing in another place, for instance ``$HOME/Library/Frameworks`` if you 273have no admin privileges on your machine, is possible. This can be accomplished 274by configuring with ``--enable-framework=$HOME/Library/Frameworks``. 275The other two directories will then also be installed in your home directory, 276at ``$HOME/Applications/Python-<VERSION>`` and ``$HOME/bin``. 277 278If you want to install some part, but not all, read the main Makefile. The 279frameworkinstall is composed of a couple of sub-targets that install the 280framework itself, the Mac subtree, the applications and the unix tools. 281 282There is an extra target frameworkinstallextras that is not part of the 283normal frameworkinstall which installs the Tools directory into 284"/Applications/Python <VERSION>", this is useful for binary 285distributions. 286 287What do all these programs do? 288============================== 289 290"IDLE.app" is an integrated development environment for Python: editor, 291debugger, etc. 292 293"Python Launcher.app" is a helper application that will handle things when you 294double-click a .py, .pyc or .pyw file. For the first two it creates a Terminal 295window and runs the scripts with the normal command-line Python. For the 296latter it runs the script in the Python.app interpreter so the script can do 297GUI-things. Keep the ``Option`` key depressed while dragging or double-clicking 298a script to set runtime options. These options can be set persistently 299through Python Launcher's preferences dialog. 300 301The program ``pythonx.x`` runs python scripts from the command line. 302Previously, various compatibility aliases were also installed, including 303``pythonwx.x`` which in early releases of Python on macOS was required to run 304GUI programs. As of 3.4.0, the ``pythonwx.x`` aliases are no longer installed. 305 306How do I create a binary distribution? 307====================================== 308 309Download and unpack the source release from https://www.python.org/download/. 310Go to the directory ``Mac/BuildScript``. There you will find a script 311``build-installer.py`` that does all the work. This will download and build 312a number of 3rd-party libraries, configures and builds a framework Python, 313installs it, creates the installer package files and then packs this in a 314DMG image. The script also builds an HTML copy of the current Python 315documentation set for this release for inclusion in the framework. The 316installer package will create links to the documentation for use by IDLE, 317pydoc, shell users, and Finder user. 318 319The script will build a universal binary so you'll therefore have to run this 320script on macOS 10.4 or later and with Xcode 2.1 or later installed. 321However, the Python build process itself has several build dependencies not 322available out of the box with macOS 10.4 so you may have to install 323additional software beyond what is provided with Xcode 2. 324It should be possible to use SDKs and/or older 325versions of Xcode to build installers that are compatible with older systems 326on a newer system but this may not be completely foolproof so the resulting 327executables, shared libraries, and ``.so`` bundles should be carefully 328examined and tested on all supported systems for proper dynamic linking 329dependencies. It is safest to build the distribution on a system running the 330minimum macOS version supported. 331 332All of this is normally done completely isolated in /tmp/_py, so it does not 333use your normal build directory nor does it install into /. 334 335Because of the way the script locates the files it needs you have to run it 336from within the BuildScript directory. The script accepts a number of 337command-line arguments, run it with --help for more information. 338 339Configure warnings 340================== 341 342The configure script sometimes emits warnings like the one below:: 343 344 configure: WARNING: libintl.h: present but cannot be compiled 345 configure: WARNING: libintl.h: check for missing prerequisite headers? 346 configure: WARNING: libintl.h: see the Autoconf documentation 347 configure: WARNING: libintl.h: section "Present But Cannot Be Compiled" 348 configure: WARNING: libintl.h: proceeding with the preprocessor's result 349 configure: WARNING: libintl.h: in the future, the compiler will take precedence 350 configure: WARNING: ## -------------------------------------------------------- ## 351 configure: WARNING: ## Report this to https://github.com/python/cpython/issues/ ## 352 configure: WARNING: ## -------------------------------------------------------- ## 353 354This almost always means you are trying to build a universal binary for 355Python and have libraries in ``/usr/local`` that don't contain the required 356architectures. Temporarily move ``/usr/local`` aside to finish the build. 357 358 359Uninstalling a framework install, including the binary installer 360================================================================ 361 362Uninstalling a framework can be done by manually removing all bits that got installed. 363That's true for both installations from source and installations using the binary installer. 364macOS does not provide a central uninstaller. 365 366The main bit of a framework install is the framework itself, installed in 367``/Library/Frameworks/Python.framework``. This can contain multiple versions 368of Python, if you want to remove just one version you have to remove the 369version-specific subdirectory: ``/Library/Frameworks/Python.framework/Versions/X.Y``. 370If you do that, ensure that ``/Library/Frameworks/Python.framework/Versions/Current`` 371is a symlink that points to an installed version of Python. 372 373A framework install also installs some applications in ``/Applications/Python X.Y``, 374 375And lastly a framework installation installs files in ``/usr/local/bin``, all of 376them symbolic links to files in ``/Library/Frameworks/Python.framework/Versions/X.Y/bin``. 377 378Weak linking support 379==================== 380 381The CPython sources support building with the latest SDK while targeting deployment 382to macOS 10.9. This is done through weak linking of symbols introduced in macOS 38310.10 or later and checking for their availability at runtime. 384 385This requires the use of Apple's compiler toolchain on macOS 10.13 or later. 386 387The basic implementation pattern is: 388 389* ``HAVE_<FUNCTION>`` is a macro defined (or not) by the configure script 390 391* ``HAVE_<FUNCTION>_RUNTIME`` is a macro defined in the relevant source 392 files. This expands to a call to ``__builtin_available`` when using 393 a new enough Apple compiler, and to a true value otherwise. 394 395* Use ``HAVE_<FUNCTION>_RUNTIME`` before calling ``<function>``. This macro 396 *must* be used a the sole expression in an if statement:: 397 398 if (HAVE_<FUNCTION>_RUNTIME) { 399 /* <function> is available */ 400 } 401 402 Or: 403 404 if (HAVE_<FUNCTION>_RUNTIME) {} else { 405 /* <function> is not available */ 406 } 407 408 Using other patterns (such as ``!HAVE_<FUNCTION>_RUNTIME``) is not supported 409 by Apple's compilers. 410 411 412Resources 413========= 414 415 * https://www.python.org/downloads/macos/ 416 417 * https://www.python.org/community/sigs/current/pythonmac-sig/ 418 419 * https://devguide.python.org/ 420