#1. create the project
poetry new --name=learn-fastapi learn-fastapi-step-by-step
Created package learn_fastapi in learn-fastapi-step-by-step
#2. Entry the project folder
cd learn-fastapi-step-by-step
#3. List the folder structure
tree
.
├── README.md
├── learn_fastapi
│ └── __init__.py
├── poetry.lock
├── pyproject.toml
└── tests
└── __init__.py
2 directories, 5 files
#4. Activate the project environment
poetry shell
Creating virtualenv learn-fastapi-4-e0vO-L-py3.11 in /Users/<user>/Library/Caches/pypoetry/virtualenvs
Spawning shell within /Users/<user>/Library/Caches/pypoetry/virtualenvs/learn-fastapi-4-e0vO-L-py3.11
source /Users/<user>/Library/Caches/pypoetry/virtualenvs/learn-fastapi-4-e0vO-L-py3.11/bin/activate.fish
Welcome to fish, the friendly interactive shell
Type `help` for instructions on how to use fish
<user>@MacBookPro ~/d/d/learn-fastapi-step-by-step> source /Users/<user>/Library/Caches/pypoetry/virtualenvs/learn-fastapi-4-e0vO-L-py3.11/bin/activate.fish
#5. add fastapi package
poetry add fastapi
#6. verify fastapi
fastapi
Usage: fastapi [OPTIONS] COMMAND [ARGS]...
FastAPI CLI - The fastapi command line app. 😎
Manage your FastAPI projects, run your FastAPI apps, and more.
Read more in the docs: <https://fastapi.tiangolo.com/fastapi-cli/>.
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --version Show the version and exit. │
│ --install-completion Install completion for the current shell. │
│ --show-completion Show completion for the current shell, to copy │
│ it or customize the installation. │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────╮
│ dev Run a FastAPI app in development mode. 🧪 │
│ run Run a FastAPI app in production mode. 🚀 │
╰──────────────────────────────────────────────────────────────────────────────╯
from fastapi import FASTAPI
app = FASTAPI()
#run in development mode
fastapi dev main.py
INFO Using path main.py
INFO Resolved absolute path /Users/<user>/Documents/Doc/learn-fastapi-step-by-step/learn_fastapi/main.py
INFO Searching for package file structure from directories with __init__.py files
INFO Importing from /Users/<user>/Documents/Doc/learn-fastapi-step-by-step
╭─ Python package file structure ─╮
│ │
│ 📁 learn_fastapi │
│ ├── 🐍 __init__.py │
│ └── 🐍 main.py │
│ │
╰─────────────────────────────────╯
INFO Importing module learn_fastapi.main
INFO Found importable FastAPI app
╭─────── Importable FastAPI app ───────╮
│ │
│ from learn_fastapi.main import app │
│ │
╰──────────────────────────────────────╯
INFO Using import string learn_fastapi.main:app
╭────────── FastAPI CLI - Development mode ───────────╮
│ │
│ Serving at: <http://127.0.0.1:8000> │
│ │
│ API docs: <http://127.0.0.1:8000/docs> │
│ │
│ Running in development mode, for production use: │
│ │
│ fastapi run │
│ │
╰─────────────────────────────────────────────────────╯
INFO: Will watch for changes in these directories: ['/Users/tony/Documents/Doc/learn-fastapi-step-by-step/learn_fastapi']
INFO: Uvicorn running on <http://127.0.0.1:8000> (Press CTRL+C to quit)
INFO: Started reloader process [45684] using WatchFiles
INFO: Started server process [45686]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:63744 - "GET / HTTP/1.1" 404 Not Found
INFO: 127.0.0.1:63746 - "GET /docs HTTP/1.1" 200 OK
INFO: 127.0.0.1:63746 - "GET /openapi.json HTTP/1.1" 200 OK
用浏览器打开127.0.0.1:8000

用浏览器打开127.0.0.1:8000/docs