# 4. 在麥金塔系統使用Python

Author

  Bob Savage <bobsavage@mac.com>

Python 執行在麥金塔系統的 Mac OS X 和執行在其他 Unix 平台上原理非常相似，但有一些值得提出的是在 Mac OS X 上增加其他額外的功能例如 IDE 與 套件管理。

## 4.1. 取得和安裝MacPython

Apple 在 Mac OS X 10.8 預設安裝 Python 2.7。但你也可以到 Python website （https://www.python.org ）更新至最新的 Python 3。Python 建立在"通用二進位"上，使 Python 能以本地程序的形式運行在使用英特爾微處理器與 PowerPC 麥金塔電腦上。

在安裝後你必須要做幾件事：

- 应用程序文件夹中的 `Python 3.8` 文件夹。在这里可以找到IDLE，即官方Python发行版的标准的开发环境；PythonLauncher，用来处理来自finder的双击python脚本；以及“build applet”工具，将python脚本打包为系统上的独立应用程序。

- A framework `/Library/Frameworks/Python.framework`, which includes the Python executable and libraries. The installer adds this location to your shell path. To uninstall MacPython, you can simply remove these three things. A symlink to the Python executable is placed in /usr/local/bin/.

The Apple-provided build of Python is installed in `/System/Library/Frameworks/Python.framework` and `/usr/bin/python`, respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software. Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do.

IDLE includes a help menu that allows you to access Python documentation. If you are completely new to Python you should start reading the tutorial introduction in that document.

If you are familiar with Python on other Unix platforms you should read the section on running Python scripts from the Unix shell.

### 4.1.1. 如何執行Python腳本

Your best way to get started with Python on Mac OS X is through the IDLE integrated development environment, see section 整合化開發工具 and use the Help menu when the IDE is running.

If you want to run Python scripts from the Terminal window command line or from the Finder you first need an editor to create your script. Mac OS X comes with a number of standard Unix command line editors, vim and emacs among them. If you want a more Mac-like editor, BBEdit or TextWrangler from Bare Bones Software (see http://www.barebones.com/products/bbedit/index.html) are good choices, as is TextMate (see https://macromates.com/). Other editors include Gvim (http://macvim-dev.github.io/macvim/) and Aquamacs (http://aquamacs.org/).

To run your script from the Terminal window you must make sure that `/usr/local/bin` is in your shell search path.

從Finder執行你的腳本時，你有兩個選項:

- Drag it to PythonLauncher

- Select PythonLauncher as the default application to open your script (or any .py script) through the finder Info window and double-click it. PythonLauncher has various preferences to control how your script is launched. Option-dragging allows you to change these for one invocation, or use its Preferences menu to change things globally.

### 4.1.2. 透過使用者圖形介面執行腳本

With older versions of Python, there is one Mac OS X quirk that you need to be aware of: programs that talk to the Aqua window manager (in other words, anything that has a GUI) need to be run in a special way. Use pythonw instead of python to start such scripts.

对于Python 3.8，可以使用 python 或 pythonw

### 4.1.3. 設定

Python on OS X honors all standard Unix environment variables such as `PYTHONPATH`, but setting these variables for programs started from the Finder is non-standard as the Finder does not read your `.profile` or `.cshrc` at startup. You need to create a file `~/.MacOSX/environment.plist`. See Apple's Technical Document QA1067 for details.

For more information on installation Python packages in MacPython, see section Installing Additional Python Packages.

## 4.2. 整合化開發工具

MacPython ships with the standard IDLE development environment. A good introduction to using IDLE can be found at http://www.hashcollision.org/hkn/python/idle_intro/index.html.

## 4.3. Installing Additional Python Packages

There are several methods to install additional Python packages:

- Packages can be installed via the standard Python distutils mode (`python setup.py install`).

- Many packages can also be installed via the setuptools extension or pip wrapper, see https://pip.pypa.io/.

## 4.4. 圖形化使用者介面(GUI) 程式開發於Mac

有許多建立圖形化使用者介面(GUI)應用程式選項使用Python於Mac上

PyObjC 是一个 Python 到 Apple 的 Objective-C/Cocoa 框架的绑定，这是大多数现代 Mac 开发的基础。 有关 PyObjC 的信息，请访问 https://pypi.org/project/pyobjc/。

The standard Python GUI toolkit is `tkinter`, based on the cross-platform Tk toolkit (https://www.tcl.tk). An Aqua-native version of Tk is bundled with OS X by Apple, and the latest version can be downloaded and installed from https://www.activestate.com; it can also be built from source.

wxPython is another popular cross-platform GUI toolkit that runs natively on Mac OS X. Packages and documentation are available from https://www.wxpython.org.

PyQt is another popular cross-platform GUI toolkit that runs natively on Mac OS X. More information can be found at https://riverbankcomputing.com/software/pyqt/intro.

## 4.5. 貢獻Python應用程式於 Mac

The "Build Applet" tool that is placed in the MacPython 3.6 folder is fine for packaging small Python scripts on your own machine to run as a standard Mac application. This tool, however, is not robust enough to distribute Python applications to other users.

The standard tool for deploying standalone Python applications on the Mac is py2app. More information on installing and using py2app can be found at http://undefined.org/python/#py2app.

## 4.6. 其他資源

MacPython 郵件清單對於Python使用者和開發者於Mac 上是一個極佳的支援資源

https://www.python.org/community/sigs/current/pythonmac-sig/

其他好用的資源是MacPython wiki:

https://wiki.python.org/moin/MacPython