4. Using Python on a Mac
************************

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

Python on a Mac running macOS is in principle very similar to Python
on any other Unix platform, but there are a number of additional
features such as the IDE and the Package Manager that are worth
pointing out.


4.1. 获取和安装 MacPython
=========================

macOS since version 10.8 comes with Python 2.7 pre-installed by Apple.
If you wish, you are invited to install the most recent version of
Python 3 from the Python website (https://www.python.org).  A current
"universal binary" build of Python, which runs natively on the Mac's
new Intel and legacy PPC CPU's, is available there.

你安装后得到的东西有：

* 会有一个 "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 IDE 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. IDE
========

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. Mac 上的图形界面编程
=========================

使用 Python 在 Mac 上构建 GUI 应用程序有多种选择。

*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. 在 Mac 上分发 Python 应用程序
==================================

在 Mac 上部署独立 Python 应用程序的标准工具是 **py2app** 。有关安装和
使用 py2app 的更多信息，请访问 http://undefined.org/python/#py2app 。


4.6. 其他资源
=============

MacPython 邮件列表是 Mac 上 Python 用户和开发人员的优秀支持资源：

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

另一个有用的资源是 MacPython wiki ：

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