ScriptsApr 13, 2026·3 min read

Poetry — Python Packaging and Dependency Management Made Easy

Poetry is a comprehensive tool for Python dependency management and packaging. It handles virtual environments, dependency resolution, lockfiles, and publishing to PyPI — all through a single, intuitive command-line interface.

TL;DR
Poetry manages Python dependencies, virtual environments, lockfiles, and publishing in a single CLI.
§01

What it is

Poetry is a comprehensive tool for Python dependency management and packaging. It handles virtual environment creation, dependency resolution with a lockfile, and package publishing to PyPI -- all through a single CLI. It replaces the fragmented workflow of pip + virtualenv + setuptools + twine.

The tool targets Python developers, library authors, and teams who want reproducible builds and a modern dependency management experience similar to npm or cargo.

§02

How it saves time or tokens

Poetry resolves dependency conflicts at install time and locks exact versions in poetry.lock. This eliminates 'works on my machine' issues and makes CI builds deterministic. The pyproject.toml file consolidates project metadata, dependencies, and build config into one place.

§03

How to use

  1. Install Poetry: curl -sSL https://install.python-poetry.org | python3 -.
  2. Create a new project: poetry new my-project or add Poetry to an existing project: poetry init.
  3. Add dependencies: poetry add requests flask and Poetry handles resolution, lockfile, and virtual environment.
§04

Example

# Install Poetry
curl -sSL https://install.python-poetry.org | python3 -

# Create a new project
poetry new my-api
cd my-api

# Add dependencies
poetry add fastapi uvicorn
poetry add --group dev pytest black mypy

# Run in the virtual environment
poetry run python -m uvicorn main:app --reload

# Build and publish to PyPI
poetry build
poetry publish
§05

Related on TokRepo

§06

Common pitfalls

  • Poetry creates a virtual environment by default. If you use Docker, set poetry config virtualenvs.create false to install directly into the system Python.
  • The lockfile (poetry.lock) should be committed to git for applications but omitted for libraries. This matches the npm convention of package-lock.json.
  • Poetry's dependency resolver can be slow for projects with many transitive dependencies. Use --no-update when adding a single package to avoid re-resolving the entire tree.

Frequently Asked Questions

How does Poetry compare to pip and requirements.txt?+

pip installs packages but does not resolve conflicts or create lockfiles. Poetry handles resolution, locking, virtual environments, and publishing. It replaces the pip + virtualenv + setuptools toolchain with a single unified tool.

Does Poetry support Python 2?+

No. Poetry requires Python 3.8 or later. It is designed for modern Python development and uses pyproject.toml, which is a Python 3 standard.

Can I use Poetry with Docker?+

Yes. In Dockerfiles, install Poetry, copy pyproject.toml and poetry.lock, run poetry install --no-dev, then copy your source code. Set virtualenvs.create false to avoid unnecessary virtual environments inside containers.

Does Poetry replace setuptools for building packages?+

Yes. Poetry uses its own build backend. You define metadata in pyproject.toml and run poetry build to create sdist and wheel distributions. Poetry publish uploads directly to PyPI.

How does Poetry handle dependency groups?+

Poetry supports named dependency groups like dev, test, and docs. Use poetry add --group dev pytest to add a dev-only dependency. Groups can be installed or excluded individually with poetry install --without dev.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets