Publishing Packages using Poetry

Publishing Packages using Poetry

Poetry is rapidly gaining recognition as an excellent dependency manager in the Python community. It has risen rapidly to become the dependency manager for various projects across the Python community. But did you know that not only can poetry be used as a dependency manager but it can also be used for publishing Python packages to PyPI? I have previously written about publishing to PyPI using the tedious method

Initiating a project

To start a new project, run the following command:

poetry new poetry-demo

This will create a file poetry-demo with the following file structure:

├── pyproject.toml
├── poetry_demo
│   └──
└── tests

NOTE: Check the availability of the name on PyPI

Adding Dependencies

To add dependencies to the project, we run the command:

poetry add <dependency>

For example, to add pandas as our dependency, we run the command:

poetry add pandas

If you are adding a dependency for the first time in a project, you will notice a new poetry.lock file being generated. It contains dependencies along with their corresponding versions and checksums/hashes. This helps users to always be on the same depency versions. It also results in faster dependency conflict resolution.

Read more about poetry

Poetry also allows us to create groups. We can do them something like this:

poetry add black isort --group dev

The installation command will now be:

pip install "poetry-demo[dev]"

Tweaking poetry.toml

The poetry.toml file contains metadata similar to the file. We can enter various things like name, version, description, license, authors, readme, repository, keywords and classifiers among other things.

Read more about the poetry.toml file

Publishing on PyPI

Before publishing our project on PyPI, We need to create a build:

poetry build

This generates a dist folder that contains a targ.gz and a whl file. Poetry works only for vanilla Python builds (no C/C++/Rust builds 😞). Now, to publish these files to PyPI, we run the command:

poetry publish

You will be prompted for your PyPI username and password. Enter them to upload the packages. Instead of running 2 sperate commands, uou could combine the 2 commands and run this instead:

poetry publish --build

And congrats, you published a library using poetry!