Poetry: new, init & add
Poetry was the first popular tool to give Python a Cargo/npm-style workflow: one command creates a project, poetry add edits pyproject.toml, resolves the whole graph, writes a lockfile and installs into a project environment. Poetry 2 (2025) moved its metadata into the standard [project] table, so a Poetry project is now an ordinary PEP 621 project with a few [tool.poetry] extras.
Creating a project
poetry new scaffolds a package with the src layout; poetry init adds a pyproject.toml to an existing folder (interactive by default; -n accepts the defaults).
poetry new greet
cd greet
tree --noreport .
cat pyproject.tomlCreated package greet in greet
.
├── README.md
├── pyproject.toml
├── src
│ └── greet
│ └── __init__.py
└── tests
└── __init__.py
[project]
name = "greet"
version = "0.1.0"
description = ""
authors = [
{name = "Ada Lovelace",email = "ada@example.com"}
]
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
]
[tool.poetry]
packages = [{include = "greet", from = "src"}]
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
Things to notice:
- The
[project]table is standard. Authors came fromgit config user.name/user.email. requires-python = ">=3.13"defaults to the running Python's version.[tool.poetry] packagestells poetry-core where the package lives (src/greet).- The backend is poetry-core, a small, separate package: anyone (pip, uv, build) can build this project without installing Poetry.
Adding dependencies
poetry add does four things: picks a version constraint, resolves the whole graph, writes poetry.lock, and installs into the project's environment (creating it on first use). In this course Poetry first needs to know about the local index; against PyPI you'd skip that line.
poetry new -q greet && cd greet
poetry source add --priority=primary local http://127.0.0.1:8080/simple/
poetry add requests
echo "--- pyproject.toml now says"
sed -n '/^dependencies/,/^]/p' pyproject.toml
ls -AAdding source with name local.
Creating virtualenv greet in /home/learner/greet/.venv
Using version ^2.34.2 for requests
Updating dependencies
Resolving dependencies...
Package operations: 5 installs, 0 updates, 0 removals
- Installing certifi (2026.10.23)
- Installing charset-normalizer (3.5.2)
- Installing idna (3.20)
- Installing urllib3 (2.8.0)
- Installing requests (2.34.2)
Writing lock file
--- pyproject.toml now says
dependencies = [
"requests (>=2.34.2,<3.0.0)"
]
.venv
README.md
poetry.lock
pyproject.toml
src
tests
- With no version given, Poetry picks the newest one and writes a caret-style range:
>=2.34.2,<3.0.0(written as PEP 508 in[project]; you can typepoetry add "requests@^2.31"or"requests>=2.31,<3"yourself). .venvappeared inside the project: this course's Poetry is configured withvirtualenvs.in-project = true. Poetry's default is a central cache folder (~/.cache/pypoetry/virtualenvs/greet-<hash>-py3.13); many teams switch to in-project venvs so editors find them.poetry config virtualenvs.in-project truedoes it.poetry.lockholds the exact resolution, explained in poetry.lock & poetry install.
Running code in the environment
You don't have to activate anything. poetry run runs a command inside the project environment, and poetry env info tells you where it is:
poetry new -q greet && cd greet
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
poetry add -q requests==2.31.0
cat > src/greet/__init__.py <<'PY'
import requests
def user_agent() -> str:
return requests.utils.default_user_agent()
PY
poetry install -q
poetry run python -c 'import greet; print(greet.user_agent())'
poetry env info --path
poetry show
echo "--- the dependency tree"
poetry show --treepython-requests/2.31.0
/home/learner/greet/.venv
certifi 2026.10.23 Python package for providing Mozilla's CA Bun...
charset-normalizer 3.5.2 The Real First Universal Charset Detector. Op...
idna 3.20 Internationalized Domain Names in Application...
requests 2.31.0 Python HTTP for Humans.
urllib3 2.8.0 HTTP library with thread-safe connection pool...
--- the dependency tree
requests 2.31.0 Python HTTP for Humans.
├── certifi >=2017.4.17
├── charset-normalizer >=2,<4
├── idna >=2.5,<4
└── urllib3 >=1.21.1,<3
poetry install also installed the project itself in editable mode, which is why import greet works. To get a shell with the environment activated, Poetry 2 prints the activation command with poetry env activate (the old poetry shell moved to a plugin).
Changing and removing
| Command | Effect |
|---|---|
poetry add "rich>=13,<15" |
add with your own constraint |
poetry add rich@latest |
bump an existing dependency to the newest version |
poetry remove requests |
remove from pyproject.toml, re-lock, uninstall it and its orphans |
poetry update requests |
re-resolve one package within its constraint and update the lock |
poetry show --outdated |
what could be updated |
poetry version minor |
bump [project] version (0.1.0 → 0.2.0) |
poetry new -q greet && cd greet
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
poetry add -q requests rich==13.9.4
poetry remove requests
poetry show --outdated
poetry version minorUpdating dependencies
Resolving dependencies...
Package operations: 0 installs, 0 updates, 5 removals
- Removing certifi (2026.10.23)
- Removing charset-normalizer (3.5.2)
- Removing idna (3.20)
- Removing requests (2.34.2)
- Removing urllib3 (2.8.0)
Writing lock file
rich 13.9.4 15.0.0 Render rich text, tables, progress bars, syntax highlight...
Bumping version from 0.1.0 to 0.2.0
Unlike pip uninstall, poetry remove requests also removed certifi, idna, urllib3 and charset-normalizer, because the environment is synced to the lockfile, not edited by hand.
Test yourself
Which build backend does a project created by poetry new use?
- setuptools
- hatchling
- poetry-core (poetry.core.masonry.api)
- Poetry itself must be installed to build it
What does poetry add requests write into [project] dependencies when the newest version is 2.34.2?
- requests==2.34.2
- requests (>=2.34.2,<3.0.0)
- requests
- requests~=2.34
Why did poetry remove requests also uninstall urllib3?
- urllib3 was broken
- Poetry syncs the environment to the lockfile, so dependencies nobody needs any more are removed
- Poetry removes all packages starting with 'u'
- It didn't