Try   HackMD

後端學習紀錄 Backend with FastAPI - Setting Enviroment

tags: backend

Copyright 2021, 月下麒麟


Target

該筆記主要目標為使用Web之Python框架FastAPI
並結合資料庫框架Flask-SQLAlchemy與資料庫SQLite
會著墨在探討FastAPI實作與前端分離應用

Advantage

  • FastAPI is a Web framework for developing RESTful APIs in Python
  • support asynchronous programming FastAPI Wiki
  • FastAPI為排行第三的Python網頁框架,僅次於Django、Flask stackoverflow 2021 Developer Survey
  • 前後端工具選用也須注意,盡量以前後的特性都一致(如:都支援異步)

Installation

Python(for Windows)

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 →

務必勾選add Python..PATH(等同設定環境變數)

Check

開啟終端機(cmd),確認Python、Pip安裝成功與否,成功即出現版本資訊

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 →

Reference: Pip是什麼? pip (軟體包管理系統)

Pip Install Module

安裝虛擬環境

pip install virtualenv

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 →

Reference:詳細請參考這篇IT邦幫忙 DAY03-搞懂Python的virtualenv

Create Vitual Enviroment

創建虛擬環境

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 →

Run Vitual Enviroment

進入資料夾

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 →

Different from Reality and Virtual

虛擬環境的好處就是作業環境可以隔離
舉個例子:
我在虛擬環境安裝了netmiko網路應用套件後

pip install netmiko

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 →

當你pip list 查看套件清單內容時會發現netmiko套件

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 →

(實際環境)

Exit Virtual Enviroment

離開虛擬環境,路徑前的小括號不見了

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 →

Package your List

假設我這個專案安裝了很多套件,
移植到別處執行程式時,總不可能一個一個確認吧~~(會瘋掉~~
所以阿~

pip 有個指令可以將該環境下的套件打包起來

pip freeze > requirement.txt

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 →

Unpckage your List

有打包,一定也有解包囉
解包時可以同時安裝requirements.txt裡的套件

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 →

這樣就會進行自動安裝了~ 是不是超方便的!

Summary

所以應該要好好區隔每個專案的作業環境,
將來你把程式打包給別人使用時,環境才會一致且具移植性