[2] | 1 |
|
---|
| 2 | .. _using-on-mac:
|
---|
| 3 |
|
---|
| 4 | ***************************
|
---|
| 5 | Using Python on a Macintosh
|
---|
| 6 | ***************************
|
---|
| 7 |
|
---|
| 8 | :Author: Bob Savage <bobsavage@mac.com>
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | Python on a Macintosh running Mac OS X is in principle very similar to Python on
|
---|
| 12 | any other Unix platform, but there are a number of additional features such as
|
---|
| 13 | the IDE and the Package Manager that are worth pointing out.
|
---|
| 14 |
|
---|
| 15 | The Mac-specific modules are documented in :ref:`mac-specific-services`.
|
---|
| 16 |
|
---|
| 17 | Python on Mac OS 9 or earlier can be quite different from Python on Unix or
|
---|
| 18 | Windows, but is beyond the scope of this manual, as that platform is no longer
|
---|
| 19 | supported, starting with Python 2.4. See http://www.cwi.nl/~jack/macpython for
|
---|
| 20 | installers for the latest 2.3 release for Mac OS 9 and related documentation.
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | .. _getting-osx:
|
---|
| 24 |
|
---|
| 25 | Getting and Installing MacPython
|
---|
| 26 | ================================
|
---|
| 27 |
|
---|
[391] | 28 | Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, you
|
---|
[2] | 29 | are invited to install the most recent version of Python from the Python website
|
---|
| 30 | (http://www.python.org). A current "universal binary" build of Python, which
|
---|
| 31 | runs natively on the Mac's new Intel and legacy PPC CPU's, is available there.
|
---|
| 32 |
|
---|
| 33 | What you get after installing is a number of things:
|
---|
| 34 |
|
---|
[391] | 35 | * A :file:`MacPython 2.7` folder in your :file:`Applications` folder. In here
|
---|
[2] | 36 | you find IDLE, the development environment that is a standard part of official
|
---|
| 37 | Python distributions; PythonLauncher, which handles double-clicking Python
|
---|
| 38 | scripts from the Finder; and the "Build Applet" tool, which allows you to
|
---|
| 39 | package Python scripts as standalone applications on your system.
|
---|
| 40 |
|
---|
| 41 | * A framework :file:`/Library/Frameworks/Python.framework`, which includes the
|
---|
| 42 | Python executable and libraries. The installer adds this location to your shell
|
---|
| 43 | path. To uninstall MacPython, you can simply remove these three things. A
|
---|
| 44 | symlink to the Python executable is placed in /usr/local/bin/.
|
---|
| 45 |
|
---|
| 46 | The Apple-provided build of Python is installed in
|
---|
| 47 | :file:`/System/Library/Frameworks/Python.framework` and :file:`/usr/bin/python`,
|
---|
| 48 | respectively. You should never modify or delete these, as they are
|
---|
| 49 | Apple-controlled and are used by Apple- or third-party software. Remember that
|
---|
| 50 | if you choose to install a newer Python version from python.org, you will have
|
---|
| 51 | two different but functional Python installations on your computer, so it will
|
---|
| 52 | be important that your paths and usages are consistent with what you want to do.
|
---|
| 53 |
|
---|
| 54 | IDLE includes a help menu that allows you to access Python documentation. If you
|
---|
| 55 | are completely new to Python you should start reading the tutorial introduction
|
---|
| 56 | in that document.
|
---|
| 57 |
|
---|
| 58 | If you are familiar with Python on other Unix platforms you should read the
|
---|
| 59 | section on running Python scripts from the Unix shell.
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | How to run a Python script
|
---|
| 63 | --------------------------
|
---|
| 64 |
|
---|
| 65 | Your best way to get started with Python on Mac OS X is through the IDLE
|
---|
| 66 | integrated development environment, see section :ref:`ide` and use the Help menu
|
---|
| 67 | when the IDE is running.
|
---|
| 68 |
|
---|
| 69 | If you want to run Python scripts from the Terminal window command line or from
|
---|
| 70 | the Finder you first need an editor to create your script. Mac OS X comes with a
|
---|
| 71 | number of standard Unix command line editors, :program:`vim` and
|
---|
| 72 | :program:`emacs` among them. If you want a more Mac-like editor,
|
---|
| 73 | :program:`BBEdit` or :program:`TextWrangler` from Bare Bones Software (see
|
---|
| 74 | http://www.barebones.com/products/bbedit/index.shtml) are good choices, as is
|
---|
| 75 | :program:`TextMate` (see http://macromates.com/). Other editors include
|
---|
| 76 | :program:`Gvim` (http://macvim.org) and :program:`Aquamacs`
|
---|
[391] | 77 | (http://aquamacs.org/).
|
---|
[2] | 78 |
|
---|
| 79 | To run your script from the Terminal window you must make sure that
|
---|
| 80 | :file:`/usr/local/bin` is in your shell search path.
|
---|
| 81 |
|
---|
| 82 | To run your script from the Finder you have two options:
|
---|
| 83 |
|
---|
| 84 | * Drag it to :program:`PythonLauncher`
|
---|
| 85 |
|
---|
| 86 | * Select :program:`PythonLauncher` as the default application to open your
|
---|
| 87 | script (or any .py script) through the finder Info window and double-click it.
|
---|
| 88 | :program:`PythonLauncher` has various preferences to control how your script is
|
---|
| 89 | launched. Option-dragging allows you to change these for one invocation, or use
|
---|
| 90 | its Preferences menu to change things globally.
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | .. _osx-gui-scripts:
|
---|
| 94 |
|
---|
| 95 | Running scripts with a GUI
|
---|
| 96 | --------------------------
|
---|
| 97 |
|
---|
| 98 | With older versions of Python, there is one Mac OS X quirk that you need to be
|
---|
| 99 | aware of: programs that talk to the Aqua window manager (in other words,
|
---|
| 100 | anything that has a GUI) need to be run in a special way. Use :program:`pythonw`
|
---|
| 101 | instead of :program:`python` to start such scripts.
|
---|
| 102 |
|
---|
[391] | 103 | With Python 2.7, you can use either :program:`python` or :program:`pythonw`.
|
---|
[2] | 104 |
|
---|
| 105 |
|
---|
| 106 | Configuration
|
---|
| 107 | -------------
|
---|
| 108 |
|
---|
| 109 | Python on OS X honors all standard Unix environment variables such as
|
---|
| 110 | :envvar:`PYTHONPATH`, but setting these variables for programs started from the
|
---|
| 111 | Finder is non-standard as the Finder does not read your :file:`.profile` or
|
---|
| 112 | :file:`.cshrc` at startup. You need to create a file :file:`~
|
---|
| 113 | /.MacOSX/environment.plist`. See Apple's Technical Document QA1067 for details.
|
---|
| 114 |
|
---|
| 115 | For more information on installation Python packages in MacPython, see section
|
---|
| 116 | :ref:`mac-package-manager`.
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | .. _ide:
|
---|
| 120 |
|
---|
| 121 | The IDE
|
---|
| 122 | =======
|
---|
| 123 |
|
---|
| 124 | MacPython ships with the standard IDLE development environment. A good
|
---|
[391] | 125 | introduction to using IDLE can be found at
|
---|
| 126 | http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html.
|
---|
[2] | 127 |
|
---|
| 128 |
|
---|
| 129 | .. _mac-package-manager:
|
---|
| 130 |
|
---|
| 131 | Installing Additional Python Packages
|
---|
| 132 | =====================================
|
---|
| 133 |
|
---|
| 134 | There are several methods to install additional Python packages:
|
---|
| 135 |
|
---|
| 136 | * Packages can be installed via the standard Python distutils mode (``python
|
---|
| 137 | setup.py install``).
|
---|
| 138 |
|
---|
[391] | 139 | * Many packages can also be installed via the :program:`setuptools` extension
|
---|
| 140 | or :program:`pip` wrapper, see http://www.pip-installer.org/.
|
---|
[2] | 141 |
|
---|
| 142 |
|
---|
| 143 | GUI Programming on the Mac
|
---|
| 144 | ==========================
|
---|
| 145 |
|
---|
| 146 | There are several options for building GUI applications on the Mac with Python.
|
---|
| 147 |
|
---|
| 148 | *PyObjC* is a Python binding to Apple's Objective-C/Cocoa framework, which is
|
---|
| 149 | the foundation of most modern Mac development. Information on PyObjC is
|
---|
| 150 | available from http://pyobjc.sourceforge.net.
|
---|
| 151 |
|
---|
| 152 | The standard Python GUI toolkit is :mod:`Tkinter`, based on the cross-platform
|
---|
| 153 | Tk toolkit (http://www.tcl.tk). An Aqua-native version of Tk is bundled with OS
|
---|
| 154 | X by Apple, and the latest version can be downloaded and installed from
|
---|
| 155 | http://www.activestate.com; it can also be built from source.
|
---|
| 156 |
|
---|
| 157 | *wxPython* is another popular cross-platform GUI toolkit that runs natively on
|
---|
| 158 | Mac OS X. Packages and documentation are available from http://www.wxpython.org.
|
---|
| 159 |
|
---|
| 160 | *PyQt* is another popular cross-platform GUI toolkit that runs natively on Mac
|
---|
| 161 | OS X. More information can be found at
|
---|
| 162 | http://www.riverbankcomputing.co.uk/software/pyqt/intro.
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | Distributing Python Applications on the Mac
|
---|
| 166 | ===========================================
|
---|
| 167 |
|
---|
[391] | 168 | The "Build Applet" tool that is placed in the MacPython 2.7 folder is fine for
|
---|
[2] | 169 | packaging small Python scripts on your own machine to run as a standard Mac
|
---|
| 170 | application. This tool, however, is not robust enough to distribute Python
|
---|
| 171 | applications to other users.
|
---|
| 172 |
|
---|
| 173 | The standard tool for deploying standalone Python applications on the Mac is
|
---|
| 174 | :program:`py2app`. More information on installing and using py2app can be found
|
---|
| 175 | at http://undefined.org/python/#py2app.
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | Other Resources
|
---|
| 179 | ===============
|
---|
| 180 |
|
---|
| 181 | The MacPython mailing list is an excellent support resource for Python users and
|
---|
| 182 | developers on the Mac:
|
---|
| 183 |
|
---|
| 184 | http://www.python.org/community/sigs/current/pythonmac-sig/
|
---|
| 185 |
|
---|
| 186 | Another useful resource is the MacPython wiki:
|
---|
| 187 |
|
---|
| 188 | http://wiki.python.org/moin/MacPython
|
---|
| 189 |
|
---|