Files
framework_note/devop_note/video/Introduction to Poetry - Python Dependency Management.md
T
2026-05-30 16:02:33 -04:00

2.7 KiB

tags, up, related, source, author, published
tags up related source author published
devop
video
poetry
python
00_devop_note
Poetry
poetry_guide
poetry_project_ideas

Poetry: Dependency Management for Python

Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Poetry offers a lockfile to ensure repeatable installs, and can build your project for distribution.

Similarities to Maven (Java)

If you are familiar with Apache Maven, you can think of Poetry as serving a similar purpose in the Python ecosystem:

Feature Maven (Java) Poetry (Python)
Project Configuration pom.xml pyproject.toml
Dependency Resolution Resolves transitive dependencies Resolves transitive dependencies with a deterministic solver
Locking No direct equivalent (uses version ranges in POM) poetry.lock (ensures exact versions)
Build & Packaging Builds JAR/WAR files Builds Wheel and sdist packages
Publishing Deploys to Central/Nexus Publishes to PyPI or private repositories
Environment Management Relies on external JRE/JDK Manages Virtual Environments automatically

Key Concepts

1. pyproject.toml

This is the single source of truth for your project. It replaces setup.py, requirements.txt, setup.cfg, MANIFEST.in and pipfile.

2. Deterministic Resolution

Poetry comes with a custom dependency resolver that will always find a solution if one exists, or clearly explain why it failed.

3. Isolation by Default

Poetry always runs in isolation. It either uses your existing virtual environment or creates its own to ensure that your project dependencies don't leak into your global Python installation.

Basic Commands

  • poetry init: Interactively create a pyproject.toml file.
  • poetry add <package>: Adds a dependency to pyproject.toml and installs it.
  • poetry install: Installs the dependencies specified in pyproject.toml (or poetry.lock if present).
  • poetry update: Updates dependencies to their latest versions according to pyproject.toml and updates the lock file.
  • poetry run <command>: Runs a command within the project's virtual environment.
  • poetry shell: Spawns a shell within the virtual environment.

Why use Poetry over Pip?

While pip is the standard package installer, it doesn't handle dependency resolution or project metadata as comprehensively as Poetry. Poetry provides a more "all-in-one" experience, much like Maven does for Java, by combining dependency management, environment isolation, and packaging into a single tool.