/Doc/using/mac.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 205 lines · 139 code · 66 blank · 0 comment · 0 complexity · 4a81a510c0d66e0b260076d94c0f0d32 MD5 · raw file

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