Getting started with ActivePython on macOS

  • Install Location: /Library/Frameworks/Python.framework/Versions/3.5 with links created in /usr/local/bin
  • Local Documentation: Open the “Help Viewer” application (/System/Library/CoreServices/Help Viewer.app). Select “ActivePython 3.5 Help” from Help Viewer’s “Library” menu.

Running Python and the Interactive Shell

The ActivePython installer on macOS installs symlinks for running python, pythonw, and pydoc to /usr/local/bin. If this directory is not already on your PATH you can add it manually as follows. Note that the default setup on macOS 10.4 (Tiger) does not put /usr/local/bin on your PATH.

Add the following statement in your ~/.bashrc file.

PATH=/usr/local/bin:$PATH; export PATH

Open the Terminal application (/Applications/Utilities/Terminal.app) and type python. (If you already had a terminal open you may need to start a new one to see the PATH changes.) You should see something like the following:

ActivePython 3.5.3.1 (ActiveState Software Inc.) based on
Python 3.5.3 (default, Mar 21 2017, 09:33:37)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

You can use the Python shell to interactively run Python code. This can be a very useful development and debugging tool. Learn how to use Python’s built-in dir() and help() introspection functions to dive into Python objects. For example, try running the following:

>>> import os.path
>>> dir(os.path)
list of members of os.path module...
>>> help(os.path)
MAN page-like desciption of the os.path module...
>>> help(os.path.join)
help on the join function...

Read the Python Tutorial for more information.

pythonw

On macOS, any process that provides a GUI (i.e. talks to the Aqua window manager) needs to be launched in a certain way. The pythonw command (just a stub that calls the actual Python executable as required) is provided for this purpose. If your Python code provides a GUI, launch it with pythonw instead of python. (There is a hack, but it is not recommended.)

GUI Programming

Tkinter is Python’s binding to the cross-platform Tk GUI toolkit. ActivePython includes Tkinter by default, but on macOS you must also install ActiveTcl to use Tkinter.

wxPython is another popular cross-platform GUI programming toolkit. It is available separately from http://www.wxpython.org/.

The PyObjC project provides a bridge between Python and Apple’s Objective-C system. Part of this system is the Cocao toolkit for macOS GUI programming. See below for more information.

macOS-specific Libraries and Tools

Documentation

In addition to the Python Library Reference that most Python developers will want to be familiar with, Python developers on macOS may also want to take a look at the Macintosh Module Reference. Please be aware, however, that some of this documentation is out-of-date (some of referring to macOS 9). The most useful sections will be MacOS Toolbox Modules and possibly MacPython OSA Modules, which discusses current bindings to Apple’s OSA (Open Scripting Architecture, of which AppleScript is an implementation) system.

PyObjC

The PyObjC project, as mentioned above, provides a bridge between Python and Apple’s Objective-C system. In particular it provides the ability to write complete macOS GUI applications in Python. PyObjC is, at the time of this writing, being very actively developed. You can either install the latest release version of PyObjC using PyPM or build it yourself as follows:

  1. If you do not have a /Developer directory, you will first need to install the Apple Developer Tools. You can download them from Apple's developer site after registering for a (free) ADC membership.
  2. You will need a Subversion client, svn, with which to checkout the latest PyObjC sources. You can find a Subversion installer here.
  3. Run the following to download, build, and install PyObjC:
    svn co http://svn.red-bean.com/pyobjc/trunk/pyobjc/
    cd pyobjc
    /usr/local/bin/python setup.py install
    

py2app

py2app (available separately from here) is an extension to Python’s standard distutils system for building and distributing Python software. If would like to distribute your Python software as full-fledged macOS applications and/or with macOS Installer packages you should take a look at py2app.