Making Python packages with poetry

About Poetry, a tool to install, manage and create packages in Python.

Poetry is a tool for creating, managing and publishing packages in Python.


Installation

May be installed from PyPI or using a script.

I prefer the PyPI version because I've heard people warning against getting accustomed to running scripts that you don't know.

PyPI

We can use pip to install poetry from PyPI.

pip install poetry

Script

Download the installation script with curl and use Python to run it.

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

Poetry's website recommends this method of installation as

If installed using this method, a sub command of poetry itself can be used to update it independent of Python version.

Create a project

poetry new project-name

By default the project created using poetry would have a flat layout.

If you prefer a src-layout, use the --src option and do

poetry new --src project-name

See [docs](https://python-poetry.org/docs/cli/#new).

Pre-existing project

The new sub-command is used when we start a new package from scratch. If you already got the files, you may use the init sub-command to generate the pyproject.toml file.

poetry init

Add dependency packages

poetry add package-name

A package can be installed as a development dependency using the --dev or -D option as in

poetry add -D package-name

Build

Build your package with

poetry build

and the built module's files would appear under the dist/ directory.

Publish

You may want to adjust the remote server to which the package is published with

poetry config repositories.pypi "https://upload.pypi.org/legacy/"   # for pypi.org

The built module can be pushed to a remote server with

poetry publish

by default the remote server is pypi.org

The package can be both built and published with

poetry --build publish