---
# System prepended metadata

title: AI LINE Bot練功坊-L3 開發環境建置
tags: [Python, Line Bot]

---

AI LINE Bot練功坊-L3 開發環境建置
===

## Python 安裝
### Windows安裝 <img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/windows8/windows8-original.svg" width=15px style="margin-bottom:5px"/>

1. 至[官網](https://www.python.org/downloads/)下載適當的版本安裝(以python3.12.2為範例)
![image](https://hackmd.io/_uploads/SygNAMd66.png)

2. 執行安裝檔(.exe)![image](https://hackmd.io/_uploads/HysJ8wua6.png)

3. 勾選「Add python.exe to PATH」，並選擇「Install Now」進行安裝![image](https://hackmd.io/_uploads/B1ujUWd66.png)

4. 出現以下畫面即為安裝完成![image](https://hackmd.io/_uploads/Hyr3PWuTp.png)

5. 將Python加入環境變數(若步驟3有勾選Add python.exe to PATH 則忽略此步驟)
    (1) 進入控制台後點選系統及安全性
        ![image](https://hackmd.io/_uploads/rytdbjOaa.png)
        
    (2) 點選系統
        ![image](https://hackmd.io/_uploads/BJca-jOpT.png)
        
    (3) 點選系統進階設定
        ![image](https://hackmd.io/_uploads/rkZKaqu6a.png)
        
    (4) 點選環境變數
        ![image](https://hackmd.io/_uploads/H1yCp5OTa.png)
        
    (5) 點選Path後點擊下方編輯
        ![image](https://hackmd.io/_uploads/HJerwjOa6.png)
        
    (6) 點選新增
        ![image](https://hackmd.io/_uploads/H1hzys_aT.png)
        
    (7) 將Python的兩個路徑新增進去(要用你自己的路徑)
    :::info
    C:\Users\USER\AppData\Local\Programs\Python\Python312\                                     C:\Users\USER\AppData\Local\Programs\Python\Python312\Scripts\
    :::
    ![image](https://hackmd.io/_uploads/S1ynvouTa.png)

6. 查看Python版本
    在命令提示字元中輸入以下指令
    ```bash
    python --version
    ```
    出現以下畫面即為安裝成功
    ![image](https://hackmd.io/_uploads/r1QAafOTp.png)


### MacOS安裝 <img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/apple/apple-original.svg" width=20px style="margin-bottom:8px "/>
          
1. 至[官網](https://www.python.org/downloads/)下載適當的版本安裝(以python3.12.2為範例)
![image](https://hackmd.io/_uploads/B1Q0OHiaa.png)



2. 執行安裝檔(.pkg)![image](https://hackmd.io/_uploads/H1YFFrjaa.png)


3. 依序安裝流程點選安裝
![image](https://hackmd.io/_uploads/HJ_aKroTT.png)
![image](https://hackmd.io/_uploads/H1YCtHia6.png)
![image](https://hackmd.io/_uploads/rJKRtBoTp.png)
![image](https://hackmd.io/_uploads/HJY0YSja6.png)
![image](https://hackmd.io/_uploads/Hkt0Frsa6.png)
![image](https://hackmd.io/_uploads/SyY0YBoTa.png)


4. 出現以下畫面即為安裝完成!![image](https://hackmd.io/_uploads/HyKxcrjpp.png)

## VScode 安裝
### Windows安裝 <img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/windows8/windows8-original.svg" width=15px style="margin-bottom:5px"/>

1. 至[官網](https://code.visualstudio.com/)下載安裝檔![image](https://hackmd.io/_uploads/rJUkcWOpa.png)

2. 執行安裝檔(.exe)![image](https://hackmd.io/_uploads/HkPrDvOTa.png)

3. 同意使用者條款
![image](https://hackmd.io/_uploads/Sk9SBXupa.png)

4. 除了"針對支援的檔案類型將Code註冊為編輯器"不要勾，其他都勾起![image](https://hackmd.io/_uploads/BkRgrX_aa.png)

5. 安裝完成會自動打開VScode
![image](https://hackmd.io/_uploads/rkjH8X_p6.png)

### MacOS安裝 <img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/apple/apple-original.svg" width=20px style="margin-bottom:8px "/>

1. 至[官網](https://code.visualstudio.com/)下載安裝檔![image](https://hackmd.io/_uploads/ryPnorj6T.png)

2. 將下載好的zip檔解壓縮![image](https://hackmd.io/_uploads/ByJRnSsTT.png)

3. 完成解壓縮後將執行檔拉進應用程式資料夾中
![image](https://hackmd.io/_uploads/rJB9aHjTT.png)

### Python Extension
1. 點選Extensions頁籤![image](https://hackmd.io/_uploads/B1jyk8o6T.png)

2. 搜尋Python套件並安裝![image](https://hackmd.io/_uploads/BJWIk8jpT.png)



## Ngrok 安裝
### Windows安裝 <img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/windows8/windows8-original.svg" width=15px style="margin-bottom:5px"/>

1. 至[ngrok官網](https://ngrok.com/)註冊帳號
![image](https://hackmd.io/_uploads/SJdxvXdap.png)

2. 登入後找到下載的頁籤
![image](https://hackmd.io/_uploads/Sy8-O7uTT.png)

3. 複製設置token指令
![image](https://hackmd.io/_uploads/SkNLdXuTp.png)

4. 解壓縮下載的檔案
![image](https://hackmd.io/_uploads/r13Ovwdp6.png)

5. 執行ngrok.exe，並貼上剛剛複製的指令
![image](https://hackmd.io/_uploads/SJRtY7_aT.png)

    :::warning
    :warning: 紅框的路徑位置必須和剛剛解壓縮後的路徑是一樣的
    :::
    
6. 成功設定token會出現紅框內容
![image](https://hackmd.io/_uploads/HkpWq7_Tp.png)

7. 建立臨時伺服器
    ```bash
    ngrok http <port>
    ```
    ![image](https://hackmd.io/_uploads/Hy9liQd66.png)

    按下Enter
    ![image](https://hackmd.io/_uploads/rk_Qomupa.png)

    臨時伺服器建立成功

### MacOS安裝 <img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/apple/apple-original.svg" width=20px style="margin-bottom:8px "/>
1. 至[ngrok官網](https://ngrok.com/)註冊帳號
![image](https://hackmd.io/_uploads/SJdxvXdap.png)

2. 登入後找到下載的頁籤
![image](https://hackmd.io/_uploads/rkuFg8o66.png)


3. 複製設置token指令
![image](https://hackmd.io/_uploads/Hy8Q-LiTa.png)


4. 解壓縮下載的檔案
![image](https://hackmd.io/_uploads/H1W9bLjpa.png)

5. 點選前往資料夾
![image](https://hackmd.io/_uploads/S1W7L8oTT.png)


6. 進入/usr/local/bin 路徑資料夾中
![image](https://hackmd.io/_uploads/B1ICIUop6.png)

7. 將解壓縮好的ngrok應用程式拉進該路徑資料夾中
![image](https://hackmd.io/_uploads/SJXfu8i6T.png)

8. 雙擊執行ngrok，直到出現［程序完成］
![image](https://hackmd.io/_uploads/Sygi9Uia6.png)

9. 開啟終端機，將複製的指令打上執行
![image](https://hackmd.io/_uploads/By8OjIiTT.png)

10. 成功設定token會出現紅框內容
![image](https://hackmd.io/_uploads/BkVijLipT.png)


11. 建立臨時伺服器
    ```bash
    ngrok http <port>
    ```
    ![image](https://hackmd.io/_uploads/SJo0sIjTT.png)


    按下Enter
    ![image](https://hackmd.io/_uploads/Hkybh8o66.png)


    臨時伺服器建立成功

## 建立Python虛擬環境
### Windows安裝 <img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/windows8/windows8-original.svg" width=15px style="margin-bottom:5px"/>

1. 建立一個空的資料夾存放虛擬環境
![image](https://hackmd.io/_uploads/ByNWhtO6p.png)

2. 打開資料夾並在上方輸入`cmd`並按下Enter
![image](https://hackmd.io/_uploads/B1vN3m_pT.png)

    這樣可以打開命令提示字元

3. 建立python虛擬環境
    ```bash
    python -m venv {環境名稱}
    ```
    ![image](https://hackmd.io/_uploads/B1PEpmdaT.png)



    執行成功後會有一個linebot的資料夾在venv中
    ![image](https://hackmd.io/_uploads/BkbXamd6a.png)

4. 需要啟動`activate.bat`才可以啟動虛擬環境

    切換路徑指令
    ```bash
    cd linebot/Scripts
    ```

    啟動activate.bat指令
    ```bash
    activate.bat
    ```

    ![image](https://hackmd.io/_uploads/rk5URQupT.png)
    這樣就成功啟動虛擬環境了
    
    
    :::warning
    :bulb: 若使用powershell的指令則為
    ```bash
    activate.ps1
    ```
    :::
    
### MacOS安裝 <img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/apple/apple-original.svg" width=20px style="margin-bottom:8px "/>

1. 建立一個空的資料夾存放虛擬環境
    ![image](https://hackmd.io/_uploads/BkRo2bi50.png)

2. 點擊左上角 **終端機 Terminal** 選項，並點選 **新增終端**
    ![image](https://hackmd.io/_uploads/H13bYzi5C.png)


3. 輸入指令，建立python虛擬環境
    ```bash
    python3 -m venv {環境名稱}
    ```
    ![image](https://hackmd.io/_uploads/rkvmtGicC.png)




4. 建立完成後輸入以下指令，切換路徑至該環境資料夾中

    ```bash
    cd {環境名稱}
    ```
    ![image](https://hackmd.io/_uploads/B1G3CroqC.png)


5. 啟動bin資料夾下的activate指令
    ```
    source bin/activate
    ```
    
   ![image](https://hackmd.io/_uploads/HyMnJUi50.png)
   ![image](https://hackmd.io/_uploads/rJ2hJUicC.png)

這樣就成功啟動虛擬環境了
     

## 安裝python套件
### Windows安裝 <img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/windows8/windows8-original.svg" width=15px style="margin-bottom:5px"/>
1. 建立專案的資料夾
    ![image](https://hackmd.io/_uploads/ByFX3Kdpa.png)

2. 在資料夾中新增`requirements.txt`
    ![image](https://hackmd.io/_uploads/HJ-dkVdaT.png)

3. 在`requirements.txt`中輸入

    ```bash
    line-bot-sdk==3.7.0
    flask==3.0.0
    ```
    ![image](https://hackmd.io/_uploads/Hye4L3Kdpp.png)

    輸入後存檔

4. 複製 `requirements.txt` 路徑
    ![image](https://hackmd.io/_uploads/rysUxEupa.png)

    ![image](https://hackmd.io/_uploads/Syfct5u6p.png)

5. 安裝套件
    ```bash
    pip install -r <requirements.txt所屬路徑>
    ```
    ![image](https://hackmd.io/_uploads/ByYAlEdTp.png)

6. 安裝完成
    ![image](https://hackmd.io/_uploads/H1ArWV_66.png)
    
    
### MacOS安裝 <img src="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/icons/apple/apple-original.svg" width=20px style="margin-bottom:8px "/>
1. 建立專案資料夾
    ![image](https://hackmd.io/_uploads/HJrONdjqR.png)

2. 開啟軟體-文字編輯器 (系統原有)
    ![image](https://hackmd.io/_uploads/S1sTb_iqC.png)

3. 點擊**新增文件**
    ![image](https://hackmd.io/_uploads/r10jfuj9A.png)

4. 點擊上方**格式>製作純文字格式**選項
    ![image](https://hackmd.io/_uploads/HJSzQ_jc0.png)

5. 將相關所需套件輸入至文件中
    ```bash
    line-bot-sdk==3.7.0
    flask==3.0.0
    ```
    ![image](https://hackmd.io/_uploads/HJM67Oi5R.png)
    
6. 輸入完成後，將檔案以`requirements.txt`存入專案資料夾中
    ![image](https://hackmd.io/_uploads/SyzlSdo5A.png)

7. 進到專案資料夾中，將`requirements.txt`檔案路徑複製起來
    ![image](https://hackmd.io/_uploads/rkODB_jqC.png)

8. 回到VSCode中，並在終端機中輸入以下指令
    ```bash
    pip install -r <requirements.txt所屬路徑>
    ```
    ![image](https://hackmd.io/_uploads/HyBWLOj9C.png)
    
9. 安裝成功
    ![image](https://hackmd.io/_uploads/Hk4rLuj5C.png)
    
 
### 查看環境套件指令
```bash
pip list
```
![image](https://hackmd.io/_uploads/By7qZV_Ta.png)

### 離開虛擬環境指令
```bash
deactivate
```
![image](https://hackmd.io/_uploads/BkekGEdaT.png)

Youtube 課程影片
---
<iframe width="560" height="315" src="https://www.youtube.com/embed/UoOv36rwmKQ?si=N7KatcRfrzu0Vjf4" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

{%hackmd @ntuebigdata/about %}

## 相關教材連結

<div style="display: flex; justify-content:space-between;">
  <div>
    <a class="btn btn-warning" href="https://hackmd.io/@ntuebigdata/create-a-line-official-account" style="color:white;width:300px;text-overflow:ellipsis;overflow:hidden">◀◀◀ L02 建立LINE Bot 官方帳號註冊 ◀◀◀</a>
  </div>
  <div>
    <a class="btn btn-info" href="https://hackmd.io/@ntuebigdata/create-an-echo-bot" style="color:white;width:300px;text-overflow:ellipsis;overflow:hidden">▶▶▶ L04 開發環境建置 ▶▶▶</a>
  </div>
</div>