LinuxCommandLibrary

poetry-add

Add project dependencies

TLDR

Add required packages

$ poetry add [package_name]
copy

Add required packages to a specific group of dependencies
$ poetry add [package_name] --group [group_name]
copy

Add a specific version of a package
$ poetry add [package_name]==[version]
copy

Add a specific version of a package equal to or earlier than a given version
$ poetry add [package_name]<=[version]
copy

Add a specific version of a package equal to or later than a given version
$ poetry add [package_name]>=[version]
copy

SYNOPSIS

poetry add [OPTIONS] [@]
poetry add [OPTIONS]
poetry add [OPTIONS]

PARAMETERS


    The name of the package to add. Can include an optional version constraint.


    Path to a local package or directory to add as a dependency.


    URL to a package distribution file (e.g., wheel, sdist) to add as a dependency.

--dev, -D
    Add the package to the dev dependency group.

--group GROUP, -G GROUP
    Add the package to a specific dependency group (e.g., test, docs).

--optional
    Mark the dependency as optional, meaning it is not installed by default.

--source SOURCE
    Specify the repository source from which to retrieve the package.

--constraint CONSTRAINT
    Manually specify a version constraint for the package.

--dry-run
    Simulate the command's execution without making any changes.

--editable
    Install a local path dependency in editable mode (--editable for pip).

--lock
    Do not perform an update of the poetry.lock file.

--allow-prereleases
    Allow prereleases for the added dependency during resolution.

--python PYTHON_VERSION
    Specify a Python version constraint for the added dependency.

--platform PLATFORM_NAME
    Specify a platform constraint for the added dependency.

DESCRIPTION

The poetry add command is a central feature of the Poetry dependency management tool for Python. It enables developers to seamlessly declare and integrate new package dependencies into their projects. Upon execution, poetry add performs several crucial steps: first, it updates the `pyproject.toml` file by adding the specified package and its version constraints. Next, it meticulously resolves the entire dependency tree to ensure compatibility and updates the `poetry.lock` file, thereby guaranteeing reproducible builds across different environments. Finally, it installs the newly added package, along with its transitive dependencies, into the project's dedicated virtual environment. This command offers extensive flexibility through various options, allowing users to categorize dependencies (e.g., development-only, specific groups), define custom sources, or incorporate packages from local paths or URLs, providing precise control over the project's dependency landscape.

CAVEATS

The poetry add command requires an existing pyproject.toml file in the current or a parent directory.
Adding a package with incompatible existing dependencies can lead to dependency resolution failures.
When specifying local paths or URLs, ensure the target provides a valid Poetry-compatible package or project structure.

<B>DEPENDENCY RESOLUTION</B>

poetry add employs a sophisticated dependency solver to ensure that all package requirements, including transitive dependencies, are compatible and resolved, effectively mitigating 'dependency hell' scenarios.

<B>REPRODUCIBLE ENVIRONMENTS</B>

Upon successful execution, poetry add automatically updates the poetry.lock file. This file precisely pins the versions of all installed packages, guaranteeing consistent and reproducible environments across different development machines and deployment stages.

HISTORY

Poetry emerged around 2018 as a comprehensive Python dependency manager, offering a more integrated approach than traditional tools. The add command has been a foundational element since its inception, continuously evolving to incorporate features like dependency groups, custom sources, and robust constraint handling, enhancing its utility for diverse project requirements.

SEE ALSO

poetry install(1), poetry update(1), poetry remove(1), poetry show(1), pip(1)

Copied to clipboard