###### tags: `Python 入門` 前置作業--安裝 Python === 學習程式設計, 最重要的就是練習, 因此在開始認識 Python 程式之前, 要先安裝好練習的環境。 ## 安裝 Python 執行環境 Python 執行環境可以在 [Python 官網](https://www.python.org/downloads/) 下載, 你可依據你的作業系統下載適當的安裝檔案。 :::warning :warning: Python 有區分 Python 2 及 Python 3 兩種版本, 本書針對的是 Python 3, 因此在選擇下載檔案時, 請不要選錯。 ::: ### Windows 作業系統 Windows 作業系統的 Python 執行環境可在 [Python Releases for Windows](https://www.python.org/downloads/windows/) 專區下載, 下載時請留意 32 或是 64 位元的版本。 :::info :memo: 本書撰寫時的 Python 版本為 3.7.2, 你下載時看到的版本可能比 3.7.2 新。 ::: 下載完成後, 執行下載的安裝檔, 會看到以下畫面 (版本號碼以實際下載檔案為準): ![](https://i.imgur.com/pry2jvY.png) 請務必記得勾選 **Add Python 3.7 to Path**, 除非有特別考量, 否則就按 **Install Now** 立即安裝。 #### 進入 Python 交談 (Interative) 模式 安裝完畢後, 請開啟**命令提示字元**, 然後鍵入 `python` 即可開啟 Python 交談模式: :::info :memo: 如果你沒有使用過**命令提示字元**, 開啟它最簡單的方式就是按鍵盤上的 `Windows` + `R`, 然後在**開啟**欄位中輸入 `cmd`。 ::: ```! C:\Users\ShinWei>python Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ``` 執行後看到的 3.7.2 就是版本編號, 最後看到的 `>>>` 是 Python 交談模式的提示符號, 表示可以在這裡鍵入程式, 讓 Python 執行對應的動作。 :::warning :warning: 如果你鍵入 `Python` 但看到的版本編號卻是 2.X.X: ```! C:\Users\ShinWei>python Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ``` 這表示你的電腦上原本就安裝有 Python 2 的環境, 為了確保可以執行 Python 3 環境, 請改成鍵入 `py -3` 指令, 例如: ```! C:\Users\ShinWei>py -3 Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ``` `py -3` 中的 `-3` 表示要執行 Python 3 環境。如果改成 `py -2` 就會變成執行 Python 2 環境。 由於不同電腦上的安裝環境可能會有差異, 建議都以 `py -3` 的方式執行 Python 3 環境。 ::: ### macOS/Linux 作業系統 macOS 與 Linux 作業系統通常都已經預先安裝有 Python 執行環境, 這裡以 Linux 為例。 #### 進入 Python 交談 (Interative) 模式 在 Linux 下, 請先開啟**終端機 (Terminal)**, 然後鍵入 `python3` 指令即可進入 Python 交談模式: ```! $ python3 Python 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ``` :::info :memo: 如果系統回覆無 `python3` 指令, 那麼就表示你的電腦上沒有安裝 Python 執行環境, 以 Debian 為基礎的 Linux 系統可以自行手動安裝: ``` $ sudo apt install python3 ``` 其他 Linux 系統請參考套件管理程式的指令安裝 Python3 套件即可。 ::: 確認已經有 Python 執行環境後, 就可以進到下一章, 開始學習 Python 程式設計了。