5. 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.

5.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.

你安裝后得到的東西有:

  • 會有一個(gè) Python 3.9 文件夾在你的 Applications 文件夾中。 在這里你可以找到 IDLE,它是作為官方 Python 發(fā)行版標(biāo)準(zhǔn)組成部分的開發(fā)環(huán)境;以及 PythonLauncher,它負(fù)責(zé)處理在 Finder 中雙擊 Python 腳本的操作。

  • 框架 /Library/Frameworks/Python.framework ,包括 Python 可執(zhí)行文件和庫。安裝程序?qū)⒋宋恢锰砑拥?shell 路徑。 要卸載 MacPython ,你可以簡單地移除這三個(gè)項(xiàng)目。 Python 可執(zhí)行文件的符號鏈接放在 /usr/local/bin/ 中。

Apple 提供的 Python 版本分別安裝在 /System/Library/Frameworks/Python.framework/usr/bin/python 中。 你永遠(yuǎn)不應(yīng)修改或刪除這些內(nèi)容,因?yàn)樗鼈冇?Apple 控制并由 Apple 或第三方軟件使用。 請記住,如果你選擇從 python.org 安裝較新的 Python 版本,那么你的計(jì)算機(jī)上將安裝兩個(gè)不同但都有用的 Python ,因此你的路徑和用法與你想要執(zhí)行的操作一致非常重要。

IDLE 包含一個(gè)幫助菜單,允許你訪問 Python 文檔。 如果您是 Python 的新手,你應(yīng)該開始閱讀該文檔中的教程介紹。

如果你熟悉其他 Unix 平臺上的 Python ,那么你應(yīng)該閱讀有關(guān)從 Unix shell 運(yùn)行 Python 腳本的部分。

5.1.1. 如何運(yùn)行 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/).

要從終端窗口運(yùn)行腳本,必須確保:file:/usr/local/bin 位于 shell 搜索路徑中。

要從 Finder 運(yùn)行你的腳本,你有兩個(gè)選擇:

  • 把腳本拖拽到 PythonLauncher

  • 選擇 PythonLauncher 作為通過 finder Info 窗口打開腳本(或任何 .py 腳本)的默認(rèn)應(yīng)用程序,然后雙擊腳本。 PythonLauncher 有各種首選項(xiàng)來控制腳本的啟動方式。 拖拽方式允許你為一次調(diào)用更改這些選項(xiàng),或使用其“首選項(xiàng)”菜單全局更改內(nèi)容。

5.1.2. 運(yùn)行有圖形界面的腳本?

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。

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 Document QA1067 for details.

更多關(guān)于在 MacPython 中安裝 Python 包的信息,參閱 安裝額外的 Python 包 部分。

5.2. IDE?

MacPython 附帶標(biāo)準(zhǔn)的 IDLE 開發(fā)環(huán)境。 有關(guān)使用 IDLE 的詳細(xì)介紹,請?jiān)L問 http://www.hashcollision.org/hkn/python/idle_intro/index.html

5.3. 安裝額外的 Python 包?

有幾個(gè)方法可以安裝額外的 Python 包:

  • 可以通過標(biāo)準(zhǔn)的 Python distutils 模式( python setup.py install )安裝軟件包。

  • 許多包也可以通過 setuptools 擴(kuò)展或 pip 包裝器安裝,請參閱 https://pip.pypa.io/ 。

5.4. Mac 上的圖形界面編程?

使用 Python 在 Mac 上構(gòu)建 GUI 應(yīng)用程序有多種選擇。

PyObjC 是一個(gè) Python 到 Apple 的 Objective-C/Cocoa 框架的綁定,這是大多數(shù)現(xiàn)代 Mac 開發(fā)的基礎(chǔ)。 有關(guān) PyObjC 的信息,請?jiān)L問 https://pypi.org/project/pyobjc/。

標(biāo)準(zhǔn)的 Python GUI 工具包是 tkinter ,基于跨平臺的 Tk 工具包( https://www.tcl.tk )。 Apple 的 OS X 捆綁了 Aqua 原生版本的 Tk ,最新版本可以從 https://www.activestate.com 下載和安裝;它也可以從源代碼構(gòu)建。

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.

5.5. 在 Mac 上分發(fā) Python 應(yīng)用程序?

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. 其他資源?

MacPython 郵件列表是 Mac 上 Python 用戶和開發(fā)人員的優(yōu)秀支持資源:

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

另一個(gè)有用的資源是 MacPython wiki :

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