新聞中心
5. 在 Mac 上使用 python
作者

專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)德令哈免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
Bob Savage
在運行 macOS 的 Mac 上的 Python 原則上與在其他 Unix 平臺上的 Python 非常相似,但有一些額外的特性,如 IDE 和包管理器,值得指出。
5.1. 獲取和安裝 MacPython
macOS used to come with Python 2.7 pre-installed between versions 10.8 and 12.3. 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.
你安裝后得到的東西有:
-
A
Python 3.12folder in yourApplicationsfolder. In here you find IDLE, the development environment that is a standard part of official Python distributions; and PythonLauncher, which handles double-clicking Python scripts from the Finder. -
框架
/Library/Frameworks/Python.framework,包括 Python 可執(zhí)行文件和庫。安裝程序?qū)⒋宋恢锰砑拥?shell 路徑。 要卸載 MacPython ,你可以簡單地移除這三個項目。 Python 可執(zhí)行文件的符號鏈接放在 /usr/local/bin/ 中。
Apple 提供的 Python 版本分別安裝在 /System/Library/Frameworks/Python.framework 和 /usr/bin/python 中。 你永遠不應(yīng)修改或刪除這些內(nèi)容,因為它們由 Apple 控制并由 Apple 或第三方軟件使用。 請記住,如果你選擇從 python.org 安裝較新的 Python 版本,那么你的計算機上將安裝兩個不同但都有用的 Python ,因此你的路徑和用法與你想要執(zhí)行的操作一致非常重要。
IDLE 包含一個幫助菜單,允許你訪問 Python 文檔。 如果您是 Python 的新手,你應(yīng)該開始閱讀該文檔中的教程介紹。
如果你熟悉其他 Unix 平臺上的 Python ,那么你應(yīng)該閱讀有關(guān)從 Unix shell 運行 Python 腳本的部分。
5.1.1. 如何運行 Python 腳本
你在 macOS 上開始使用 Python 的最好方法是通過 IDLE 集成開發(fā)環(huán)境,參見章節(jié) IDE 以及在IDE運行時使用幫助菜單。
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 (https://macvim-dev.github.io/macvim/) and Aquamacs (http://aquamacs.org/).
要從終端窗口運行腳本,必須確保:file:/usr/local/bin 位于 shell 搜索路徑中。
要從 Finder 運行你的腳本,你有兩個選擇:
-
把腳本拖拽到 PythonLauncher
-
選擇 PythonLauncher 作為通過 finder Info 窗口打開腳本(或任何 .py 腳本)的默認應(yīng)用程序,然后雙擊腳本。 PythonLauncher 有各種首選項來控制腳本的啟動方式。 拖拽方式允許你為一次調(diào)用更改這些選項,或使用其“首選項”菜單全局更改內(nèi)容。
5.1.2. 運行有圖形界面的腳本
對于舊版本的 Python ,有一個需要注意的 macOS 特殊問題:與 Aqua 窗口管理器通信的程序(換句話說,任何有 GUI 的程序)需要以特殊的方式運行。使用 pythonw 而不是 python 來啟動此類腳本。
對于 Python 3.9,你可以使用 python 或者 pythonw。
5.1.3. 配置
macOS 上的 Python 遵循所有標準的 Unix 環(huán)境變量,例如 PYTHONPATH ,但是為從 Finder 啟動的程序設(shè)置這些變量是不標準的,因為 Finder 在啟動時不會讀取你的 .profile 或 .cshrc 。你需要創(chuàng)建一個文件 ~/.MacOSX/environment.plist 。詳情請看蘋果的技術(shù)文件QA1067。
更多關(guān)于在 MacPython 中安裝 Python 包的信息,參閱 安裝額外的 Python 包 部分。
5.2. IDE
MacPython 附帶標準的 IDLE 開發(fā)環(huán)境。 有關(guān)使用 IDLE 的詳細介紹,請訪問 http://www.hashcollision.org/hkn/python/idle_intro/index.html 。
5.3. 安裝額外的 Python 包
有幾個方法可以安裝額外的 Python 包:
-
可以通過標準的 Python distutils 模式(
python setup.py install)安裝軟件包。 -
許多包也可以通過 setuptools 擴展或 pip 包裝器安裝,請參閱 https://pip.pypa.io/ 。
5.4. Mac 上的圖形界面編程
使用 Python 在 Mac 上構(gòu)建 GUI 應(yīng)用程序有多種選擇。
PyObjC 是一個 Python 到 Apple 的 Objective-C/Cocoa 框架的綁定,這是大多數(shù)現(xiàn)代 Mac 開發(fā)的基礎(chǔ)。 有關(guān) PyObjC 的信息,請訪問 https://pypi.org/project/pyobjc/。
標準的 Python GUI 工具包是 tkinter ,基于跨平臺的 Tk 工具包( https://www.tcl.tk )。 Apple 的 OS X 捆綁了 Aqua 原生版本的 Tk ,最新版本可以從 https://www.activestate.com 下載和安裝;它也可以從源代碼構(gòu)建。
wxPython 是另一個流行的跨平臺 GUI 工具包,可以在 macOS 上原生運行。軟件包和文檔可從 https://www.wxpython.org 獲取。
PyQt 是另一個流行的跨平臺 GUI 工具包,可以在 MacOS 上原生運行。更多信息可以在 https://riverbankcomputing.com/software/pyqt/intro 找到。
5.5. 在 Mac 上分發(fā) Python 應(yīng)用程序
在 Mac 上部署獨立 Python 應(yīng)用程序的標準工具是 py2app 。關(guān)于安裝和使用 py2app 的更多信息可以在 https://pypi.org/project/py2app/ 找到。
5.6. 其他資源
MacPython 郵件列表是 Mac 上 Python 用戶和開發(fā)人員的優(yōu)秀支持資源:
https://www.python.org/community/sigs/current/pythonmac-sig/
另一個有用的資源是 MacPython wiki :
https://wiki.python.org/moin/MacPython
文章題目:創(chuàng)新互聯(lián)Python教程:5.在Mac上使用Python
地址分享:http://www.5511xx.com/article/cdisicc.html


咨詢
建站咨詢
