Last active
May 8, 2023 08:44
-
-
Save relyky/a25637195302d53394ff799e92375f3e to your computer and use it in GitHub Desktop.
python, pip, Django, FastAPI, unicorn 相關 CLI 指令整理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 檢查版本 | |
python --version # python 版本 | |
python -m pip --version # pip 版本 | |
python -m django --version # django 版本 | |
django-admin --version # django-admin 版本 | |
## 用 django 建立第一個網站 | |
django-admin startproject mysite # 建立 Django website 並指定名稱為 mysite。 | |
cd mysite | |
python manage.py runserver # 啟動 Django website。 | |
python manage.py runserver 8080 # 啟動 Django website 並指定 port number。 | |
python manage.py migrate # 應該是相依模組間的版本匹配與調和吧 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 這是 FastAPI 的第一個練習。 | |
# | |
from fastapi import FastAPI | |
app = FastAPI() | |
@app.get("/") | |
async def root(): | |
return {"message": "hello world. 哈囉世界。"} | |
@app.get('/book/{book_id}') | |
def get_book_by_id(book_id: int): | |
return { | |
'book_id': book_id | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fastapi | |
uvicorn[standard] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python -V # 檢查 Python 版本 | |
python -m venv env # 建立 Python 執行虛擬環境 | |
.\env\Scripts\activate # 切入 Python 虛擬空間 | |
...[requirements.txt]... # 編輯需要的相依套件 | |
python -m pip install --upgrade pip # 可能需先更新 pip 版本。 | |
pip install -r .\requirements.txt # 安裝需要的相依套件,依據 requirements.txt 套件需求清單來安裝。(有點像 npm 的 package.json 檔) | |
...[main.py]... # 開發 Web Api 進入點。一般進入檔名叫 main.py ;進入物件叫 app。 | |
uvicorn main:app # 啟動 Web API 並產生 Swagger 文件。 | |
# 其中 "mian:app" 是指: | |
# (1)使用 main.py 檔建立 WebApi。 | |
# (2)啟動物件名稱為 app。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 檢查版本 | |
python --version # python 版本 | |
python -m pip --version # pip 版本 | |
pip --version # pip 版本,也有效。 | |
python -m django --version # django 版本 | |
## 安裝 Django | |
pip install Django==4.2.1 # 安裝 Django 並指定版號 v4.2.1 | |
## 更新套件 | |
python.exe -m pip install --upgrade pip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment