5. 在 Mac 上使用 Python¶
- 作者:
Bob Savage <bobsavage@mac.com>
运行 macOS 的 Mac 上的 Python 在原则上与其他 Unix 平台上的 Python 非常相似,但有一些额外的特性 如集成开发环境(IDE)和包管理器值得指出。
5.1. 获取和安装 Python¶
macOS 曾经在 10.8 至 12.3 版中预装了 Python 2.7。 建议你从 Python 网站 安装最新版本的 Python 3。 那里也提供了当前的 Python "universal2 binary" 编译版,它可以在 Mac 的新式 Apple Silicon 和旧式 Intel 处理器上原生运行。
你安装后得到的东西有:
你的
Applications
文件夹中会有一个Python 3.11
文件夹。 你将在这里找到 IDLE,它是作为官方 Python 发行版标准组成部分的开发环境;以及 Python Launcher,它负责处理在 Finder 中双击 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 Python, you can remove these three things. A symlink to the Python executable is placed in/usr/local/bin/
.
备注
On macOS 10.8-12.3, 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.
如果你熟悉其他 Unix 平台上的 Python ,那么你应该阅读有关从 Unix shell 运行 Python 脚本的部分。
5.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 nano among them. If you want a more Mac-like editor, BBEdit from Bare Bones Software (see https://www.barebones.com/products/bbedit/index.html) are good choices, as is TextMate (see https://macromates.com). Other editors include MacVim (https://macvim.org) and Aquamacs (https://aquamacs.org).
要从终端窗口运行脚本,必须确保:file:/usr/local/bin 位于 shell 搜索路径中。
要从 Finder 运行你的脚本,你有两个选择:
Drag it to Python Launcher.
Select Python Launcher as the default application to open your script (or any
.py
script) through the finder Info window and double-click it. Python Launcher 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.
5.1.2. 运行有图形界面的脚本¶
对于旧版本的 Python ,有一个需要注意的 macOS 特殊问题:与 Aqua 窗口管理器通信的程序(换句话说,任何有 GUI 的程序)需要以特殊的方式运行。使用 pythonw 而不是 python 来启动此类脚本。
对于 Python 3.9,你可以使用 python 或者 pythonw。
5.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 Q&A QA1067
for details.
For more information on installation Python packages, see section 安装额外的 Python 包.
5.2. IDE¶
Python ships with the standard IDLE development environment. A good introduction to using IDLE can be found at https://www.hashcollision.org/hkn/python/idle_intro/index.html.
5.3. 安装额外的 Python 包¶
本小节已移至 Python Packaging User Guide。
5.4. GUI Programming¶
使用 Python 在 Mac 上构建 GUI 应用程序有多种选择。
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 是另一个流行的跨平台 GUI 工具包,可以在 macOS 上原生运行。软件包和文档可从 https://www.wxpython.org 获取。
PyQt 是另一个流行的跨平台 GUI 工具包,可以在 MacOS 上原生运行。更多信息可以在 https://riverbankcomputing.com/software/pyqt/intro 找到。
PySide is another cross-platform Qt-based toolkit. More information at https://www.qt.io/qt-for-python.
5.5. Distributing Python Applications¶
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/.
5.6. 其他资源¶
The Pythonmac-SIG mailing list is an excellent support resource for Python users and developers on the Mac:
https://www.python.org/community/sigs/current/pythonmac-sig/
另一个有用的资源是 MacPython wiki :