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