Try   HackMD

PyCharm 安裝教學 (for Mac User)

流程

  1. HomeBrew
  2. python3
  3. PyCharm
  4. 開啟 PyCharm 架設 fastAPI 框架

安裝HomeBrew

  1. 先確認有沒有安裝Xcode
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

如果macOS版本比較舊?
要不要用官方的GUI安裝

  1. 在終端機輸入
    ​​​​/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. 如果提示說要安裝 Command Line Tools,請執行
    ​​​​xcode-select --install
    

如果遇到HomeBrew安裝問題,可參考此篇

利用 HomBrew 安裝 python3

  1. 在終端機輸入
    ​​​​brew install python3
    
  2. 檢查是否安裝成功
    ​​​​python3 --version
    

下載並安裝PyCharm

  1. 官網下載安裝檔
  2. 選擇屬於你的CPU的安裝檔,Intel CPU 請選擇.dmg (intel),M1或M2的用戶請選擇.dmg (Apple Silicon)
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  3. 等待安裝的同時,可以辦一個帳戶登入網站
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  4. 創建帳戶,設定密碼
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  5. 跳轉畫面後,可以利用學校帳號開通專業版
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  6. 點選 Apply now
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  7. 填寫基本資料,學校信箱建議使用gapps,才收得到認證信件
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  8. 待認證後,就擁有專業版帳號
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  9. 回到帳號管理頁面,點選上方"Your Account"
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  10. 點選右方選單中的"Linked email"
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  11. 填入你剛剛填的學校信箱,並到信箱中點選連結驗證
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  12. 再回到帳號管理頁面,如果有出現下圖,代表License匯入成功
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  13. 點選上圖紅色勾勾處按鈕進行啟用
  14. 拉到最下方後,點選"Accept"
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

開啟 PyCharm 安裝 fastAPI 框架

在每次開新專案時,都必須安裝對應的套件,因為在PyCharm裡,每個專案都有屬於他自己的虛擬環境。

  1. 開啟PyCharm

  2. 在開啟後登入已認證過的帳戶 (無教學圖示)

  3. 建立測試專案,點選 New Project

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  4. 選擇 Pure Python 並設定好存放專案的路徑,按下 Create

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  5. 簡單介紹環境,中斷點不是錯誤提示,執行按鈕在右上角

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  6. 按下執行按鈕,執行程式碼

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  7. 進入終端機

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  8. 在終端機輸入以下指令,安裝 fastAPI

    ​​​​pip3 install fastapi
    

    註:皆為小寫,且是用 pip3 指令下載

  9. 繼續在終端機輸入以下指令,安裝 uvicorn 是搭配 fastAPI 在開方上的輔助工具

    ​​​​pip3 install "uvicorn[standard]"
    
  10. 測試,撰寫以下 python 程式碼

    ​​​​from typing import Union ​​​​from fastapi import FastAPI ​​​​app = FastAPI() ​​​​@app.get("/") ​​​​def read_root(): ​​​​ return {"Hello": "World"} ​​​​@app.get("/items/{item_id}") ​​​​def read_item(item_id: int, q: Union[str, None] = None): ​​​​ return {"item_id": item_id, "q": q}
    1. 在終端機輸入以下指令
    ​​​​uvicorn main:app --reload
    
    1. 在終端機的結果當中點選網址,查看測試結果

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

    2. 結果

      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

過程中若遇到問題,可以參考官方文檔