Python-FastAPI-Poetry-Starter
Requirements:
- Python (^3.12)
- FastAPI (^0.104.1)
- Uvicorn (extras = [“standard”], version = “^0.29.0”)
- Gunicorn (^21.2.0)
- Pydantic (^2.5.2)
- SqlAlchemy (^2.0.30)
- Asyncpg (^0.29.0)
- Pydantic-settings (^2.2.1)
- Python-dotenv(^1.0.1)
Dev
- Mypy
- AutoPep8
- Isort
- Pytest
- Pytest-cov
- Alembic
Poetry workflow
Install poetry package manager or read official manual.
pipx install poetry
After install all project dependencies (.env creates in project folder)
poetry install
Activate local env with poetry
poetry shell
And run your app with uvicorn
uvicorn src.main:app --host=0.0.0.0 --port 8080 --reload
Note For deactivate poetry shell run in terminal
deactivate
Poetry dependecies workflow
1) Show current env dependencies and latest packages
poetry show -l
2) Update all dependencies and write poetry.lock
poetry update
3) Update installed package in latest version (group optional)
poetry add <pakage_name>@latest
4) Write poetry.lock into legacy requirements.txt
poetry export -o requirements.txt
Docker workflow
Install docker on your local mashine.
First you build docker image, you can named it by yourself (my variant: fastapi-rest)
docker build -t fastapi-rest -f Dockerfile.dev .
After you can run your project in docker container with params port: 8080, volume (listen all changes in local and copy to container), image (fastapi-rest)
docker run -p 8080:8080 -v ./src:/app/src -dt fastapi-rest
Alembic workflow
Prepare Alembic
Before create migrations import your orm Base, as target_metadata = Base.metadata alembic/env.py
1) Create migration
alembic revision --autogenerate -m "Prepare Alembic"
After revision checkout migrations in alembic/versions/*
2) Commit changes upgrade/downgrade run commad
alembic upgrade head
Common workflow
1) Add your all orm models only import and run revision with own text
alembic revision --autogenerate -m "Your message"
After revision checkout migrations in alembic/versions/*
2) Commit changes upgrade/downgrade run commad
alembic upgrade head