DEV Community

Cover image for Installing POETRY - the intuitive python dependency manager
John Johnson Okah
John Johnson Okah

Posted on • Updated on

Installing POETRY - the intuitive python dependency manager

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.


INSTALL on Linux & macOS

curl -sSL https://install.python-poetry.org | python3 -
Enter fullscreen mode Exit fullscreen mode

VERIFY Installation

poetry --version
Enter fullscreen mode Exit fullscreen mode

UPDATE Poetry itself

poetry self update
Enter fullscreen mode Exit fullscreen mode

UNINSTALL Poetry

curl -sSL https://install.python-poetry.org | python3 - --uninstall
Enter fullscreen mode Exit fullscreen mode

Setup NEW Project

poetry new poetry-demo
Enter fullscreen mode Exit fullscreen mode

PS: poetry-demo should be the name of your project

...the command creates a directory poetry-demo with the following content:

poetry-demo
├── pyproject.toml
├── http://README.md
├── poetry_demo
│   └── __init__.py
└── tests
    └── __init__.py
Enter fullscreen mode Exit fullscreen mode

PS: pyproject.toml is the most important file here. And it looks like this:

[tool.poetry]
name = "poetry-demo"
version = "0.1.0"
description = ""
authors = ["JJOKAH <jjokah.pm.me>"]
readme = "README.md"
packages = [{include = "poetry_demo"}]

[tool.poetry.dependencies]
python = "^3.9"

[tool.poetry.dev-dependencies]
black = "^21.9b0"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Enter fullscreen mode Exit fullscreen mode

INTIALISE a pre-existing project

cd pre-existing-project
Enter fullscreen mode Exit fullscreen mode
poetry init
Enter fullscreen mode Exit fullscreen mode

PS: this interactively creates a pyproject.toml file in the directory - pre-existing-project


SPECIFY and ADD dependencies

...in the pyproject.toml file under the tool.poetry.dependencies section, specify your package like this:

[tool.poetry.dependencies]
pendulum = "^2.1"
Enter fullscreen mode Exit fullscreen mode

...OR use this command:

poetry add pendulum
Enter fullscreen mode Exit fullscreen mode

VERSION constraints

...in the example above pendulum="^2.1" means
install the pendulum package with a version greater than 2.1.0 and less than 3.0.0 (>=2.1.0 <3.0.0)


DEV dependencies

specify with --dev to add a package for the development environment only and not in Production:

poetry add black --dev
Enter fullscreen mode Exit fullscreen mode

... black package will be added to the tool.poetry.dev-dependencies section

[tool.poetry.dev-dependencies]
black = "^21.9b0"
Enter fullscreen mode Exit fullscreen mode

INSTALL dependencies

poetry install
Enter fullscreen mode Exit fullscreen mode

UPDATE dependencies

poetry update
Enter fullscreen mode Exit fullscreen mode

ACTIVATE the virtual environment

poetry shell
Enter fullscreen mode Exit fullscreen mode

DEACTIVATE / exit the environment

exit
Enter fullscreen mode Exit fullscreen mode

Now, we are all set with #python poetry 🥂.

Top comments (0)