Poetry

What is Poetry?

Poetry is a dependency and package manager for Python. It simplifies project management by handling dependencies, virtual environments, and packaging in a structured way. Unlike pip and requirements.txt, Poetry provides a centralized configuration (pyproject.toml) and ensures consistent dependency resolution.

Why Use Poetry?

  • Simplifies dependency management: Handles installing/updating dependencies efficiently.

  • Manages virtual environments: Creates an isolated environment for the project.

  • Improves reproducibility: Ensures the same dependencies are installed across systems.

  • Supports publishing: Makes it easy to publish packages to PyPI.


Step-by-Step Poetry Setup for Your reverse-name-app

Step 1: Install Poetry

If you haven’t installed Poetry yet, run:

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

Or, for macOS with Homebrew:

brew install poetry

After installation, verify:

poetry --version

Step 2: Navigate to Your Project Directory

Move to the reverse-name-app folder:


Step 3: Initialize a Poetry Project

Run:

It will prompt you to enter:

  • Package name (default: reverse-name-app)

  • Version (default: 0.1.0)

  • Description

  • Author name and email

  • License

  • Dependencies (skip for now by pressing Enter)

Alternatively, use:

This creates a pyproject.toml file.


Step 4: Add Dependencies

Your app.py needs Flask, so install it with:

This automatically updates pyproject.toml and creates poetry.lock.


Step 5: Activate the Virtual Environment

Poetry manages a virtual environment. To enter it:


Step 6: Run Your Flask App

Inside the Poetry shell, start your app:

Or, if using Flask's built-in server:


Step 7: (Optional) Add Development Dependencies

For testing/debugging tools, use:


Step 8: Lock and Export Dependencies

To ensure the same dependencies install everywhere:

For non-Poetry users (export requirements.txt):


Step 9: Deactivating Virtual Environment

Exit Poetry's virtual environment:


Step 10: Run Without Activating Poetry Shell

Instead of poetry shell, you can use:


Step 11: (Optional) Publish the Project

If needed:


That’s it! Your reverse-name-app now runs with Poetry. 🚀

Last updated