Getting started with ActivePython on macOS
- Install Location:
/Library/Frameworks/Python.framework/Versions/2.7with links created in
- Local Documentation: Open the “Help Viewer” application (
/System/Library/CoreServices/Help Viewer.app). Select “ActivePython 2.7 Help” from Help Viewer’s “Library” menu.
Running Python and the Interactive Shell
The ActivePython installer on macOS installs symlinks for running
/usr/local/bin. If this directory is not already on your PATH you can add it manually as follows.
Add the following statement in your
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 184.108.40.206 (ActiveState Software Inc.) based on Python 2.7.14 (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
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.
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.)
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
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.
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:
If you do not have a
/Developerdirectory, 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.
You will need a Subversion client,
svn, with which to checkout the latest PyObjC sources. You can find a Subversion installer here.
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 (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