Poetry: Dependency Management and Packaging in Python

Bhavik Jikadara
3 min readJul 8, 2024

Dependency management and packaging can be a daunting task for Python developers. Poetry is here to make that process easier and more efficient. This article will explore what Poetry is, how it works, its pros and cons, and provide a step-by-step guide on installing and using it in a real project like web scraper, Q&A, Retrieval-Augmented Generation (RAG) chatbot.

What is Poetry?

Poetry is a robust tool for managing dependencies and packaging in Python. It aims to simplify the process of maintaining project dependencies, creating virtual environments, and packaging code for distribution.

How Does Poetry Work?

Poetry uses a pyproject.toml file to manage project metadata, dependencies, and settings. This file replaces the traditional requirements.txt and setup.py files, providing a unified configuration.

Here’s a breakdown of its workflow:

  1. Initialization: Poetry initializes a new project with all the necessary files.
  2. Dependency Management: Add, update, or remove dependencies with simple commands.
  3. Environment Management: Poetry creates and manages virtual environments.
  4. Packaging: Easily build and publish your project.

Pros and Cons of Poetry

Pros

  • Unified Configuration: One file for dependencies and project settings.
  • Automatic Virtual Environment Management: No need for venv or virtualenv.
  • Simplified Dependency Resolution: Handles complex dependencies automatically.
  • Built-in Publishing Tools: Simplifies the process of publishing packages to PyPI.

Cons

  • Learning Curve: New users may need time to adapt to Poetry’s workflow.
  • Compatibility: May face issues with some legacy projects or tools.

Installation

Follow these steps to install Poetry:

  • Install Poetry: Run the following command in your terminal:
curl -sSL https://install.python-poetry.org | python3 -
  • Alternatively, you can use pip:
pip install poetry
  • Configure Poetry: Add Poetry to your PATH by updating your shell configuration file (.bashrc, .zshrc, etc.):
export PATH="$HOME/.poetry/bin:$PATH"

Structure of Project:

my-package
├── pyproject.toml
├── README.md
├── src
│ └── my_package
│ └── __init__.py
└── tests
└── __init__.py

Example Project: Simple Web Scraper

Install Poetry (if not already installed):

curl -sSL https://install.python-poetry.org | python3 -

Create a New Project:

poetry new web-scraper 
cd web-scraper

Add Dependencies: Open the pyproject.toml file created by Poetry. Add the required dependencies under the [tool.poetry.dependencies] section. For example, if you're using requests for making HTTP requests and beautifulsoup4 for parsing HTML:

[tool.poetry]
name = "web-scraper"
version = "0.1.0"
description = "A simple web scraper project"
authors = ["Your Name <your.email@example.com>"]

[tool.poetry.dependencies]
python = "^3.8"
requests = "^2.25.1"
beautifulsoup4 = "^4.9.3"
  • Alternatively, you can add dependencies via the command line:
poetry add requests beautifulsoup4

Install Dependencies: Run the following command to install the specified dependencies:

poetry install
  • Poetry will create a virtual environment and install all the dependencies specified in pyproject.toml.

Activate the Virtual Environment: Poetry automatically manages the virtual environment for your project. You can activate it using:

poetry shell

Create a Python Script: Create a Python script, for example, scraper.py:

import requests
from bs4 import BeautifulSoup

def scrape_website(url):
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
return soup.title.string
else:
return "Failed to retrieve content"

if __name__ == "__main__":
url = "https://example.com"
title = scrape_website(url)
print(f"Website Title: {title}")

Run the Script: Run your script using Poetry’s environment:

poetry run python scraper.py

Conclusion

Poetry offers a streamlined approach to dependency management and packaging in Python. By integrating Poetry into your workflow, you can simplify project setup, manage dependencies more effectively, and focus on coding. With this guide, you’re ready to harness the power of Poetry in your projects, including sophisticated applications.

Happy coding!

--

--

Bhavik Jikadara
Bhavik Jikadara

Written by Bhavik Jikadara

🚀 AI/ML & MLOps expert 🌟 Crafting advanced solutions to speed up data retrieval 📊 and enhance ML model lifecycles. buymeacoffee.com/bhavikjikadara

No responses yet