Using Poetry to manage Python packages: A practical guide (with examples)

Updated: January 29, 2024 By: Guest Contributor Post a comment

Introduction

Python developers understand the importance of managing packages and dependencies in a seamless and efficient manner. Traditional tools like pip and virtualenv have been instrumental in the Python environment, but as projects grow, dependency management can become increasingly complex. Poetry enters as a solution that aims to simplify the process. This guide provides a practical approach to using Poetry with clear examples to manage Python packages efficiently.

What is Poetry?

Poetry is an all-in-one tool for dependency management and packaging in Python. It intends to replace pip and virtualenv by offering a single, unified command-line tool that manages both packages and virtual environments. With Poetry, developers can declare project dependencies, install them, manage virtual environments, and package their project for distribution, all through one cohesive interface.

Features of Poetry:

  • Dependency resolution: Poetry handles dependency resolution and installation.
  • Virtual environments: It automatically creates and manages virtual environments.
  • Package publishing: It makes it easy to build and publish packages to repositories.
  • Version management: Poetry respects semantic versioning and helps manage dependencies accordingly.

Installation of Poetry

To install Poetry, run the following command in your terminal:

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

On Windows, you can use Powershell:

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -

Verify Installation

After installing, you can verify the installation with this command:

poetry --version

This will output the installed version of Poetry, confirming the successful installation.

Starting a New Project with Poetry

Initializing a new Python project with Poetry is straightforward:

poetry new my-project

This command will create a new directory called ‘my-project’ with the necessary files, including a pre-populated ‘pyproject.toml’ – the file used by Poetry for dependency management.

Defining Dependencies

Inside ‘pyproject.toml’, you will define your project’s dependencies. To add dependencies to your project, you can use the ‘add’ command:

poetry add requests

This will add the ‘requests’ package as a dependency and update ‘pyproject.toml’ accordingly.

Installing Dependencies

With the dependencies defined, install them with the following command:

poetry install

This reads ‘pyproject.toml’, resolves the dependencies, and installs them in the project’s virtual environment.

Updating Dependencies

Updates to dependencies can be made with the ‘update’ command:

poetry update

This will update the dependencies to their latest versions acceptable by the version constraints defined in ‘pyproject.toml’.

Managing Virtual Environments

Poetry automatically handles virtual environments for your projects. To create a virtual environment, you can simply run:

poetry shell

After activation, you will be placed in the virtual environment shell, isolated from your global Python environment.

Publishing Packages

Poetry can also be used to publish packages. To publish a package, use:

poetry publish --build

This command builds the package and publishes it to the specified package repository. By default, Poetry uses the Python Package Index (PyPI).

Advanced Configuration

‘pyproject.toml’ also allows for further customization and configuration of many aspects of the packaging and release process, such as specifying the Python version compatibility, scripts, and more.

Here’s an example of a pyproject.toml file that you can use with Poetry, demonstrating various configurations like Python version compatibility, dependency specification, and scripts. I’ll include explanations as comments within the code:

[tool.poetry]
name = "your-package-name"
version = "0.1.0"
description = "A brief description of your package"
authors = ["Your Name <[email protected]>"]

# Specify the Python version(s) your package is compatible with
[tool.poetry.dependencies]
python = "^3.8"

# Dependencies for your package
# Example: requests version 2.25 or higher but below 3.0
requests = "^2.25"

# Development dependencies
[tool.poetry.dev-dependencies]
pytest = "^6.0"

# Custom scripts
[tool.poetry.scripts]
your-script = "your_package.module:main_function"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

Conclusion

Poetry addresses key challenges in Python package management and aims to make the process more intuitive and reliable. Through the scope of this guide, we covered its installation, basic usage in creating a project, adding and updating dependencies, managing virtual environments, and publishing packages. With this tool, Python developers can look forward to a streamlined workflow that helps maintain a clear and consistent approach to package management.