Member-only story
Avoid the Snake Pit of Python Package Management With Poetry
Using Poetry to build private and public Python libraries
Python is a programming language that brings joy to a lot of developers. Unfortunately, building and using libraries in Python has always been a struggle. Multiple tools exist that try to help (to name a few: setuptools
, pip
, virtualenv
, pipenv
, tox
, and conda
) but each one has its own quirks and limitations.
In this article, we look at Poetry
, a relatively new package management tool that tries to bring the same joy of programming in Python, to building and using Python libraries.
Poetry in practice
The steps below show you how to build a library with Poetry, how to use that library from another Python project, and how to distribute the library for public reuse on PyPi.
Step 1. Installing Poetry
The Poetry website contains extensive documentation on how to install it on different operating systems.
I’ve installed Poetry on my MacBook through Homebrew:
$ brew install poetry
$ poetry --version
Poetry version 1.1.11
Step 2. Creating a small Python library from scratch
Let’s write a tiny library using Poetry. This library will be called PyInitials
and its single purpose is to return initials for given full names, for example, “Guido van Rossum” => “GvR”.
First, initialize a new library project with Poetry:
$ poetry new pyinitials
Created package pyinitials in pyinitials
Next, create a new virtual environment and install the initial set of dependencies:
$ cd pyinitials
[pyinitials] $ poetry install
Creating virtualenv pyinitials-Q6tcgwA_-py3.9 in /Users/rob/Library/Caches/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies... (1.0s)Writing lock filePackage operations: 8 installs, 0 updates, 0 removals• Installing pyparsing (2.4.7)
• Installing attrs (21.2.0)
• Installing more-itertools (8.10.0)
• Installing packaging (21.0)
• Installing…