[Avg. reading time: 6 minutes]

Poetry

A Dependency & Environment Manager

Poetry simplifies dependency management and packaging in Python projects.

Create a new project:

poetry new helloworld

Sample layout of the directory structure

helloworld/
├── pyproject.toml
├── README.md
├── helloworld/
│   └── __init__.py
└── tests/
    └── __init__.py
  1. Navigate to your project directory
cd helloworld

Windows Users (Recommended Approach)

Working with Virtual Environments

  1. Create and activate a virtual environment:
poetry env activate
  1. Get Virtual Python Interpreter info, to verify the base Python vs Poetry env
poetry env info

Or Use this one line.

poetry env use  $(poetry env info -e)
  1. Verify the Virtual env libraries. You will notice only pip.
poetry run pip list
  1. Add project dependencies:
poetry add faker
  1. Create a main.py under src/hellworld/ (subfolder)

main.py

from faker import Faker
fake = Faker()
print(fake.name())
  1. Run program
poetry run python src/helloworld/main.py

Managing Your Project

  1. View all installed dependencies:
poetry show
  1. Update dependencies:
poetry update
  1. Remove a dependency:
poetry remove package-name

Key Benefits

  1. Simplified Environment Management
  • Poetry automatically creates and manages virtual environments
  • No need to manually manage pip and virtualenv
  1. Clear Dependency Specification
  • All dependencies are listed in one file (pyproject.toml)
  • Dependencies are automatically resolved to avoid conflicts
  1. Project Isolation
  • Each project has its own dependencies
  • Prevents conflicts between different projects
  1. Easy Distribution
  • Package your project with a single command:
poetry build

Publish to PyPI when ready:

poetry publish

Best Practices

  • Always activate virtual environment before working on project
  • Keep pyproject.toml updated with correct dependencies
  • Use version constraints wisely
  • Commit both pyproject.toml and poetry.lock files to version control

Poetry Setup

#python #poetry #ruff #lint #mypyVer 5.5.3

Last change: 2025-10-15