4. 在 Mac 系統使用Python
************************

作者:
   Bob Savage <bobsavage@mac.com>

Python 執行在 Mac 的 macOS 和執行在其他 Unix 平台上原理非常相似，但值
得一提的是在 macOS 上有其他額外的功能，例如 IDE 與 套件管理。


4.1. 取得和安裝 MacPython
=========================

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

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

* 会有一个 "Python 3.9" 文件夹在你的 "Applications" 文件夹中。 在这里
  你可以找到 IDLE，它是作为官方 Python 发行版标准组成部分的开发环境；
  以及 PythonLauncher，它负责处理在 Finder 中双击 Python 脚本的操作。

* 框架 "/Library/Frameworks/Python.framework" ，包括 Python 可执行文件
  和库。安装程序将此位置添加到 shell 路径。 要卸载 MacPython ，你可以
  简单地移除这三个项目。 Python 可执行文件的符号链接放在
  /usr/local/bin/ 中。

Apple 提供的 Python 版本分别安装在
"/System/Library/Frameworks/Python.framework" 和 "/usr/bin/python" 中
。 你永远不应修改或删除这些内容，因为它们由 Apple 控制并由 Apple 或第
三方软件使用。 请记住，如果你选择从 python.org 安装较新的 Python 版本
，那么你的计算机上将安装两个不同但都有用的 Python ，因此你的路径和用法
与你想要执行的操作一致非常重要。

IDLE 包含一个帮助菜单，允许你访问 Python 文档。 如果您是 Python 的新手
，你应该开始阅读该文档中的教程介绍。

如果你熟悉其他 Unix 平台上的 Python ，那么你应该阅读有关从 Unix shell
运行 Python 脚本的部分。


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

Your best way to get started with Python on macOS 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. macOS 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/).

要从终端窗口运行脚本，必须确保:file:*/usr/local/bin* 位于 shell 搜索路
径中。

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

* 把脚本拖拽到 **PythonLauncher**

* 选择 **PythonLauncher** 作为通过 finder Info 窗口打开脚本（或任何
  .py 脚本）的默认应用程序，然后双击脚本。 **PythonLauncher** 有各种首
  选项来控制脚本的启动方式。 拖拽方式允许你为一次调用更改这些选项，或
  使用其“首选项”菜单全局更改内容。


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

With older versions of Python, there is one macOS 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.9，你可以使用 **python** 或者 **pythonw**。


4.1.3. 設定
-----------

Python on macOS 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.

更多关于在 MacPython 中安装 Python 包的信息，参阅 安装额外的 Python 包
部分。


4.2. 整合化開發工具
===================

MacPython 附带标准的 IDLE 开发环境。 有关使用 IDLE 的详细介绍，请访问
http://www.hashcollision.org/hkn/python/idle_intro/index.html 。


4.3. 安装额外的 Python 包
=========================

有几个方法可以安装额外的 Python 包：

* 可以通过标准的 Python distutils 模式（ "python setup.py install" ）
  安装软件包。

* 许多包也可以通过 **setuptools** 扩展或 **pip** 包装器安装，请参阅
  https://pip.pypa.io/ 。


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

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

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

标准的 Python GUI 工具包是 "tkinter" ，基于跨平台的 Tk 工具包（
https://www.tcl.tk ）。 Apple 的 OS X 捆绑了 Aqua 原生版本的 Tk ，最新
版本可以从 https://www.activestate.com 下载和安装；它也可以从源代码构
建。

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

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


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

The standard tool for deploying standalone Python applications on the
Mac is **py2app**. More information on installing and using py2app can
be found at https://pypi.org/project/py2app/.


4.6. 其他資源
=============

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

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

其他好用的資源是MacPython wiki:

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