1. 程式人生 > Python入門教學 >02 MacOS 下搭建 Python 開發環境

02 MacOS 下搭建 Python 開發環境

既然要學習 Python,那麼肯定要在電腦上安裝 Python。這節課我們就來學習下如何在 MacOS 的作業系統下搭建我們的 Python 執行環境:

1. MacOS 的 Python 執行環境

1.1 Python 2 與 Python 3

Python 有兩個主要的版本: Python 2 與 Python 3。Python 3 相對於 Python 2 是一個重大的升級,Python 3 與 Python 2 兩者的語法不相容。最近這幾年 Python 3 逐漸普及、使用率不斷上升,Python 2 即將逐步退出歷史舞臺。因此,新手學習 Python 應該選擇 Python 3,在安裝 Python 時請注意選擇安裝 Python 3

圖片描述

Python 2與Python 3

1.2 32 位 Python 與 64 位 Python

  • 32 位作業系統與 64 位作業系統

MacOS 作業系統分為 32 位作業系統與 64 位作業系統。在 32 位作業系統中,CPU 一次能處理 32 位的資料,支援記憶體最多為 4 GB (2 的 32 次方);在 64 位作業系統中,CPU 一次能處理 64 位的資料,支援記憶體可以超過 4 GB (2 的 32 次方)。通常情況下,記憶體超過 4 GB 時,應該選擇安裝 64 位作業系統。

  • 32 位 Python 與 64 位 Python

根據作業系統的位數,Python 分為 32 位 Python 與 64 位 Python。在 32 位作業系統中,只能執行 32 位 Python;在 64 位作業系統中,既能執行 32 位 Python 也能執行 64 位 Python。

1.3 MacOS 對 Python 的支援

MacOS 是一套運行於蘋果電腦上的作業系統。MacOS 的核心與 Linux 的核心存在相似之處,尤其是在命令列環境下,在兩個作業系統之間存在大量的類似命令和軟體。在 MacOS 環境中的 Python 使用方法與在 Linux 環境中的 Python 使用方法幾乎完全相同。

MacOS 對 Python 的支援具有如下特點:

  • MacOS 已經自帶了 Python 2 軟體,但是沒有自帶 Python 3 軟體。因為 Python 2 即將退出歷史舞臺,Python 3 是學習和應用的主流,因此使用者需要單獨安裝 Python 3 軟體。

  • 在 MacOS 中,Python 2 的程式名稱為 python,Python 3 的程式名稱為 python3。而在 Windows 中,Python 2 與 Python 3 的程式名稱均為 python。目前關於 python 的教程、視訊都是針對 Python 3,因此,使用者在 MacOS 環境中學習 python 時,請使用命令 python3 進入 Python 環境

  • 從 2018 年初開始,MacOS 系統開始全面轉向 64 位應用了,蘋果公司要求所有提交至 MacOS 應用商店的應用程式都必須更新至 64 位。因此在新版本的 MacOS 中,只有 64 位的 Python。

2. 安裝步驟

2.1 安裝 Homebrew

Homebrew 是執行在 MacOS 下的一款軟體包管理工具,提供了諸如安裝、解除安裝、更新、檢視、搜尋等功能,功能類似於 Ubuntu Linux 的安裝包管理工具 apt。

使用命令列方式安裝 Homebrew,在 MacOS 的終端中輸入:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

以上命令首先使用 curl 命令從網站 raw.githubusercontent.com 下載 Homebrew 的安裝指令碼,安裝指令碼是用 Ruby 語言編寫的,然後使用 Ruby 執行該安裝指令碼安裝 Homebrew。執行安裝指令碼時,螢幕輸出如下資訊:

==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew...==> The following new directories will be created:
/usr/local/bin
/usr/local/etc
/usr/local/include
/usr/local/lib...==> /usr/bin/sudo /bin/mkdir -p /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/sbin /usr/local/share /usr/local/var /usr/local/opt /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var/homebrew /usr/local/var/homebrew/linked /usr/local/Cellar /usr/local/Caskroom /usr/local/Homebrew /usr/local/Frameworks
Password:...==> Installation successful!

安裝指令碼在執行過程中要求使用者輸入密碼,獲得管理員許可權後在系統目錄下建立檔案。當螢幕出現 “Installation successful!” 表示安裝成功。

2.2 安裝 Python 3

首先,使用 ‘brew search’ 命令搜尋軟體倉庫的軟體,確認軟體倉庫存在軟體 python3,如下所示:

$ brew search python3==> Formulae
boost-python3       python3             python@3            [email protected]

然後,使用 ‘brew install’ 命令從軟體倉庫中下載並安裝 Python 3,如下所示:

$ brew install python3
Updating Homebrew...==> Installing dependencies for python: gdbm, [email protected], readline, sqlite and xz==> Installing python dependency: gdbm==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.18.1.high_sierra.b 
==> python
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

If you need Homebrew's Python 2.7 run
  brew install python@2

You can install Python packages with
  pip3 install <package>They will install into the site-package directory
  /usr/local/lib/python3.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python

從以上輸出可以看到 Python 3 最終的路徑為 /usr/local/bin/python3,Python 3 的程式名稱為 python3 而不是 python

3. 在 MacOS 中執行 Python

3.1 執行 Python

Python 存在兩個版本 Python 2 和 Python 3,在 MacOS 中,命令 python 啟動的是 Python 2,命令 python 3 啟動的是 Python 3。

在命令列中輸入命令 python,啟動 Python 2,如下所示:

圖片描述

進入 Python 2

Python 2 啟動時,在第一行輸出資訊包括 “Python 2.7.16”,表明當前使用的 Python 版本是 Python 2。

3.2 執行 Python3

在命令列中輸入命令 python3,啟動 Python 3,如下所示:

圖片描述

進入 Python 3

Python 3 啟動時,在第一行輸出資訊包括 “Python 3.7.6”,表明當前使用的 Python 版本是 Python 3。**在 Ubuntu 中,請使用命令 python3 而不是命令 python 進入 Python 環境。 **

3.3 退出 Python

啟動 Python 命令後,輸入命令 exit() 可以退出 Python,請注意輸入 exit() 而不是 exit,如下所示:

圖片描述

退出 Python

3.4 執行 IDLE

IDLE 是 Python 官方提供的 Python 整合開發環境,可以在 IDLE 中完成編寫和執行 Python 程式。IDLE 的功能簡單容易上手,適合初學者入門使用。在 MacOS 中,Python 的安裝包帶有 IDLE,安裝完 Python 時,同時也安裝了 IDLE。

Python 存在兩個版本 Python 2 和 Python 3,在 MacOS 中,命令 idle 啟動的是 Python 2 版本的 IDLE,命令 python 3 啟動的是 Python 3 版本的 IDLE。

在命令列中輸入命令 idle,啟動 Python 2 版本的 IDLE,IDLE 啟動後顯示 “Python 2.7.16”,如下所示:

圖片描述

Python 2 自帶的整合開發環境 IDLE

在命令列中輸入命令 idle3,啟動 Python 3 版本的 IDLE,IDLE 啟動後顯示 “Python 3.7.6”,如下所示:
圖片描述

Python 3 自帶的整合開發環境 IDLE

4. 小結

到這裡,在 MacOS 下安裝 Python 已經成功了,需要注意的是 Python 存在兩個版本 Python 2 和 Python 3,在 MacOS 中,命令 python 啟動的是 Python 2,命令 python 3 啟動的是 Python 3。現在的情勢 Python 3 正在慢慢取代 Python 2。所以,本教程也會使用 Python 3 版本來進行講解學習。