Skip to content

Blog rovingdev

Go back

Poetry : A Better Alternative to requirements.txt. The Maven for Python ?

Edit page

On a Friday evening, a Python team deployed updates using requirements.txt, only to face a production crash caused by untracked transitive dependency changes. Debugging revealed mismatched local environments, unpinned versions, and undocumented Python constraints.

Rollback attempts failed due to a breaking change in another dependency, and onboarding new developers was already chaotic. Dependency drift across staging and production led to unpredictable failures, forcing the team into a weekend firefight. They realized the lack of a lockfile and proper dependency management with requirements.txt had caused critical downtimeβ€”and vowed to switch to Poetry.

What this tutorial will have ? πŸ€”:

  1. What is Poetry
  2. Similarities with Maven
  3. Why it is superior to requirements.txt

Poetry:

  1. Poetry is a tool used to manage and organize the various components required to build and run a python application. It’s essentially a way to ensure that all the necessary pieces are in place and working together seamlessly.

  2. Imagine building a house. You need various materials like bricks, cement, and wood, and you need to ensure that they are all the right quality and quantity. You also need to make sure that they are all delivered to the construction site at the right time. This is similar to what Poetry does, but instead of building a house, it’s helping to build a software application.

Poetry official logo

Poetry official logo

Poetry takes care of the following tasks:

Poetry is a modern, all-in-one tool for managing Python projects, designed to address common challenges in dependency management, virtual environments, and project packaging. Unlike traditional workflows that rely on pip, setup.py, and requirements.txt, Poetry simplifies and unifies these processes. It also simplifies publishing to repositories.

Sample Code:

This example creates a python project which will have a library function for conversion of parquet file to JSON file. This can also be run independently at CLI by accepting input path of parquet file and output path to store JSON file.

Prerequisites:

Terminal window
Python 3.9.x
git 2.x
pip 22.x

Installation:

Terminal window
pip install pipx
pipx install poetry
poetry config virtualenvs.in-project true

virtualenvs.in-project true will create .venv inside project, this will make life of IDEs easier and can easily identify project dependencies via project virtual env.

Project Creation:

Terminal window
[tool. poetry]
name = "parquet2json"
version = "0.1.0"
description = ""
authors = ["sukumaar"]
readme = "README.md"
packages = [include = "parquet2json" }]
[tool.poetry.dependencies]
python = "^3.13"
polars = "1.18.0"
argparse = "1.4.0"
[build-system]
requires = ["poetry-core=1.9.1"]
build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
parquet2json = 'parquet2json.__main__:main'

Project directory structure:

Terminal window
parquet_to_json_example
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
β”œβ”€β”€ parquet2json
β”‚ β”œβ”€β”€ __init__.py
β”‚ β”œβ”€β”€ __main__.py
β”‚ └── converter
β”‚ β”œβ”€β”€ __init__.py
β”‚ └── parquet_to_json.py
β”œβ”€β”€ pyproject.toml
└── tests
└── __init__.py

Poetry Commands:

Command for this project:

Terminal window
## git clone, and build
git clone gitagithub.com:sukumaar/parquet_to_json_example.git
cd parquet_to-json_example
poetry config virtualenvs. in-project true poetry install
poetry build #packages will be created under dist/
## test, change path-to-project to correct path
cd ~
mkdir test
cd test
pip uninstall parquet2json -y
pip install --no-cache-dir path-to-project/dist/parquet2json-o.1.0-py3-none-any.whl
## use like below
parquet2json -h # for help
parquet2json --i "path/to/parquet" --o "path/for/json"

Sounds similar to Maven from Java world ?

Similarities between Poetry (Python) and Maven (Java) :

By looking at above tutorial you could think that there are many similarities between poetry and maven, some of them are listed below:

But my team uses requirements.txt why should I look into other solutions ?

Features of Poetry and why it is better than requirements.txt:

Poetry official logo

Why Poetry Matters for Large Organizations

Poetry should be preferred in large teams. Implementing Poetry can significantly benefit large organizations, especially those with numerous developers working on critical projects. Here are some key reasons:

  1. Enhanced Collaboration 🀝
    1. Consistency Across Teams
    2. Easier Onboarding
  2. Simplified Dependency Management πŸ“¦
    1. Automatic Resolution
    2. Version Control
    3. Easy Dependency Management
  3. Streamlined Project Setup πŸ› οΈ
    1. Quick Start, poetry simplifies the setup process for new projects
    2. Unified Configuration
    3. Reproducibility
  4. Deployment Made Easy βš™οΈ
    1. One Command Installation
    2. Rollback Capabilities

Edit page
Share this post on:

Previous Post
Dataform: The ELT Game Changer for GCP
Next Post
Guide to : Java Spring Boot Docker Container Image