Poetry: Python packaging and dependency management made easy

Resources

Introduction

From poetry’s github: poetry is a tool to handle dependency installation as well as building and packaging of Python packages. It only needs one file to do all of that: the new, standardized pyproject.toml.

In other words, poetry uses pyproject.toml to replace setup.py, requirements.txt, setup.cfg, MANIFEST.in and Pipfile.

Tab completion can also be added with poetry completions bash >> ~/.bash_completion.

Installation

When installing packages it’s always best to look at the official documentation. However, when writing the installation process was very simple,

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

Once installed it can be updated with poetry self update.

Usage

A Python project can easily be initiated with poetry new project-name. This will yield the following directory structure,

project-name
├── pyproject.toml
├── README.md
├── project_name
│   └── __init__.py
└── tests
    └── __init__.py

A virtual environment can be initiated with poetry shell.

The pyproject.toml file handles the whole project. Instead of adding dependencies to the file you can run, e.g., poetry add graphene to add the graphene package as a project dependency. Dependencies can be installed with poetry install.

The run command executes the given command inside the project’s virtual environment. E.g., you can run a script file with poetry run python main.py.

With poetry build you can package your library into sdist (source format) and wheel (compiled format). Now your ready to publish your library. You can publish directly to PyPI with poetry publish.