PYTHON
First steps with FastAPI and Poetry, revisited after Poetry 2
In February 2025 I posted a FastAPI walkthrough with Poetry. A month earlier, Poetry 2 had taken two commands the post used out of its core. This is the version that works today.
In February 2025 I posted here a walkthrough for starting an API with FastAPI, using Poetry to handle the virtual
environment and dependencies. It was based on Bill Lubanovic’s book FastAPI: Modern Python Web Development. What I
hadn’t noticed is that Poetry 2.0, released on January 5, 2025, had taken two commands the post used out of its core:
poetry shell and poetry export. Anyone following the text with the new Poetry ran into “command not found”.
What you need
- Python 3. FastAPI stopped accepting 3.7 at the end of 2023 (version 0.104), so the original post, which asked for “3.7 or later”, was already behind. Check the minimum version on the FastAPI page before starting.
- Poetry. The way the documentation recommends is installing it with
pipx, which keeps Poetry isolated from the rest:
pipx install poetry
poetry --version
The project
mkdir fastapi-app
cd fastapi-app
poetry init -n
poetry add fastapi "uvicorn[standard]"
-n accepts the default answers of init. Since this is an application and not a library to publish, it’s worth
adding to pyproject.toml:
[tool.poetry]
package-mode = false
Without it, a later poetry install complains that it couldn’t find the fastapi_app package to install.
The application
main.py:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"message": "Bem-vindo ao FastAPI!"}
@app.get("/hello/{name}")
def saudacao(name: str):
return {"message": f"Olá, {name}!"}
name: str isn’t just an annotation: FastAPI uses the type to validate the input and to build the documentation.
Running without poetry shell
In 2025 I wrote to enter the environment with poetry shell and then run Uvicorn. In Poetry 2, shell became a
separate plugin. The way that works in any version is asking Poetry to run the command inside the environment:
poetry run uvicorn main:app --reload
The application is at http://127.0.0.1:8000, and the interactive documentation, generated from the types, at
http://127.0.0.1:8000/docs.
A shortcut that often shows up in tutorials and doesn’t work is putting the command in [tool.poetry.scripts]:
[tool.poetry.scripts]
start = "uvicorn main:app --reload"
That section expects a Python entry point in the module:function format, not a terminal command. poetry run start
fails.
Testing from the terminal
With the server running, curl is enough:
curl http://127.0.0.1:8000/hello/Mundo
The original suggested installing HTTPie as a project dependency. A command-line tool isn’t a dependency of the
application; if you want HTTPie, install it with pipx, like Poetry.
Exporting the dependencies
poetry export, which generates a requirements.txt, also left the core in Poetry 2. It is now a plugin:
poetry self add poetry-plugin-export
poetry export -f requirements.txt --output requirements.txt
References
- FastAPI, first steps
- Poetry 2.0: what changed
- Lubanovic, B. FastAPI: Modern Python Web Development. O’Reilly, 2023.
Who writes this
Eduardo Lira
Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.