Building and Publishing Packages with Poetry

In this tutorial we will be looking at a nice python tool for building and publishing python packages and libraries. Poetry is a python dependency management tool that enables us to manage several python dependencies on your system. It comes with the ability to create virtual environment just like pipenv.

Just like pipenv, it manages and tracks your dependencies using a .lock file (poetry.lock) which is similar to pipfile.lock.

Poetry makes it easier to both build and publish your package with just one file. Unlike the normal procedure of using a setup.py file, requirements.txt or pipfile, it uses only one file – pyproject.toml file.

This simplifies the entire python package development

Let us start on how to build a package. In this case we will be converting our ML model into a reusable package for others to benefit[ To learn more check this course].

Installation

First of all we will need to install venv for creating our virtual environment if your system doesn’t already have.

 

sudo apt-get install python3-venv
# For Python 3.7
sudo apt-get install python3.7-venv

Installing Poetry

curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python

Or you can use pip too

pip install --user poetry

Creating a New Project Environment

To create a new project we can use

poetry new name_of_your_pkg

This will create a new folder with the necessary files and folders.

It will also create a pyproject.toml file which is the most important file which we will use to configure and manage our build and package.

We can either use the interactive aspect of the CLI to customize the pyproject.toml file or we can do it manually.

To use the interactive CLI you can use

poetry init

We can customize and add the LICENCE, Readme and the repository as well as our main code.

You can then check for errors using

poetry check

Building the Package Files,wheel files

Poetry makes it easier to create wheel files using the command

poetry build

This will create wheel files ready to be deployed or published

Publishing Our Package

You will need to create an account on test.pypi and pypi – the python package index. Then with this command below you can config your account and publish first to the test.pypi

poetry config repositories.testpypi https://test.pypi.

Then you can publish it using

poetry publish -r test.pypi

Publishing to PyPi

If our test.pypi works perfectly we can now publish it to our official pypi account using

poetry publish

It ask you for your username and some details  and boom! , you are done you package is live on PyPI.

If you are quite certain of your package, you can even skip publishing it first to testPyPI and just go with PyPI.

To learn more on building useful packages with poetry you can check this course here

 

Thanks for your time

Jesus Saves

By Jesse E. Agbe

 

Leave a Comment

Your email address will not be published. Required fields are marked *