LinuxCommandLibrary

poetry

Manage Python dependencies and packaging

TLDR

Create a new Poetry project in the directory with a specific name

$ poetry new [project_name]
copy

Install and add a dependency and its sub-dependencies to the pyproject.toml file in the current directory
$ poetry add [dependency]
copy

Install the project dependencies using the pyproject.toml file in the current directory
$ poetry install
copy

Interactively (append -n for non-interactively) initialize the current directory as a new Poetry project
$ poetry init
copy

Get the latest version of all dependencies and update poetry.lock
$ poetry update
copy

Execute a command inside the project's virtual environment
$ poetry run [command]
copy

Bump the version of the project in pyproject.toml
$ poetry version [patch|minor|major|prepatch|preminor|premajor|prerelease]
copy

Spawn a shell within the project's virtual environment (for versions below 2.0, use poetry shell)
$ eval "$(poetry env activate)"
copy

SYNOPSIS

poetry [--version] [--help] [--quiet | -q] [--verbose | -v | -vv | -vvv] [--ansi] [--no-ansi] [--no-interaction] [--profile] [--directory DIRECTORY] command [options] [arguments]

Common usage examples:
poetry new my-project
poetry add requests
poetry install
poetry run python my_script.py
poetry build
poetry publish

PARAMETERS

--version
    Displays the application's version.

--help
    Displays help for the current command or a specific subcommand.

--quiet, -q
    Do not output any message.

--verbose, -v, -vv, -vvv
    Increase the verbosity of messages: -v for normal output, -vv for more verbose output, and -vvv for debug output.

--ansi
    Force ANSI output (for colored text).

--no-ansi
    Disable ANSI output (for plain text).

--no-interaction
    Do not ask any interactive question (useful for scripting).

--profile
    Display timing and memory usage for a command.

--directory DIRECTORY
    The working directory for the Poetry project, if different from the current directory.

DESCRIPTION

Poetry is a Python dependency management and packaging tool that aims to simplify and streamline the development workflow for Python projects. It manages your project's dependencies, creates isolated virtual environments, builds distributions, and publishes packages to repositories like PyPI.

Unlike traditional tools like pip and setuptools used separately, Poetry provides an all-in-one solution with a focus on deterministic dependency resolution and ease of use. It utilizes a `pyproject.toml` file (PEP 518) to declare project metadata and dependencies, and generates a `poetry.lock` file to ensure reproducible builds across different environments.

Its robust dependency solver ensures that compatible versions of all dependencies are installed, preventing common version conflicts. Poetry significantly enhances productivity for Python developers by automating many tedious aspects of project setup, dependency handling, and packaging, making it a popular choice for modern Python development.

CAVEATS

Poetry is not a standard Linux command-line utility but a third-party Python application. It must be installed separately, typically via methods like pipx or pip. While powerful, it introduces a specific workflow that might differ from traditional pip and venv setups, requiring some initial adaptation. It is recommended to install Poetry itself in an isolated environment (e.g., using pipx) to avoid conflicts with system Python installations or project dependencies.

INSTALLATION

Poetry is typically installed using pipx, which ensures it's isolated from your project dependencies:
pipx install poetry
Alternatively, it can be installed directly via pip:
pip install poetry

KEY CONFIGURATION FILES

Projects managed by Poetry primarily use two files in the project root:
pyproject.toml: Defines project metadata, dependencies, and build system (PEP 518/517). This file serves as the central configuration for a Poetry project.
poetry.lock: A lock file generated by Poetry that records the exact versions of all dependencies and sub-dependencies, ensuring reproducible builds across different machines and environments.

HISTORY

Poetry was created by Sławomir Gawroński with the goal of improving Python dependency management and packaging. It emerged as a response to the challenges of managing project dependencies and virtual environments with existing tools like pip, virtualenv, and setuptools individually.

Initially released around 2018, Poetry quickly gained traction for its innovative approach, particularly its robust dependency resolver and its adoption of `pyproject.toml` (PEP 518) for project metadata and configuration. It provided a holistic solution that combined dependency resolution, virtual environment management, building, and publishing into a single CLI tool, offering a more consistent and user-friendly experience than its predecessors. Its development continues, focusing on stability, performance, and adherence to modern Python packaging standards.

SEE ALSO

pip(1), pipenv(1), python(1) (specifically python -m venv)

Copied to clipboard