Back to Home
    Python PoetryPython Poetry v1.8Beginner

    Python Poetry

    Poetry is Python's all-in-one dependency management and packaging tool — declare dependencies, manage virtual envs, and publish packages from a single pyproject.toml.

    7 min read
    pythonpoetrypackage-managervirtual-envpackaging

    Installation & Setup

    2 topics

    Install Poetry

    bash
    # Official installer — keeps Poetry isolated from your Python envs
    curl -sSL https://install.python-poetry.org | python3 -
    
    # Verify the install
    poetry --version
    
    # Enable tab completion (bash example)
    poetry completions bash >> ~/.bash_completion

    💡 Never install Poetry with pip into a project venv — use the official installer or pipx

    📌 The installer puts the binary at ~/.local/bin/poetry — make sure that's on your PATH

    Configure Poetry

    bash
    # Store virtualenvs inside the project directory (.venv/) instead of the global cache
    # Useful for IDE auto-discovery and Docker layer caching
    poetry config virtualenvs.in-project true
    
    # See all current config values
    poetry config --list
    
    # Set the PyPI token for publishing (stored securely in keyring)
    poetry config pypi-token.pypi <your-token>

    💡 virtualenvs.in-project true is the most IDE-friendly setting

    📌 Per-project config lives in pyproject.toml; global config in ~/.config/pypoetry/config.toml

    Project Management

    2 topics

    Create a New Project

    bash
    # Scaffold a new project with a standard layout
    poetry new my-project
    
    # Scaffold with src/ layout (recommended for packages)
    poetry new --src my-project
    
    # Initialise Poetry in an existing directory interactively
    cd existing-project
    poetry init

    💡 Use --src layout when building an installable package to avoid import confusion

    📌 poetry init walks you through pyproject.toml creation without creating files

    pyproject.toml Structure

    toml
    [tool.poetry]
    name = "my-project"
    version = "0.1.0"
    description = "A short description"
    authors = ["Your Name <[email protected]>"]
    readme = "README.md"
    
    [tool.poetry.dependencies]
    python = "^3.11"          # minimum Python version constraint
    requests = "^2.31"        # caret: compatible with 2.31.*
    pydantic = { version = "^2.0", extras = ["email"] }  # with extras
    
    [tool.poetry.group.dev.dependencies]
    pytest = "^8.0"
    ruff = "^0.4"
    
    [build-system]
    requires = ["poetry-core"]
    build-backend = "poetry.core.masonry.api"

    💡 Caret ^ means 'compatible release' — ^2.31 allows >=2.31.0 <3.0.0

    📌 Group dev dependencies under [tool.poetry.group.dev.dependencies] to exclude them from production installs

    Dependency Management

    3 topics

    Add & Remove Dependencies

    bash
    # Add a runtime dependency (updates pyproject.toml + poetry.lock)
    poetry add requests
    
    # Add with a version constraint
    poetry add 'django>=4.2,<5'
    
    # Add a dev-only dependency (won't be installed in production)
    poetry add --group dev pytest ruff
    
    # Add a package with extras (e.g. async support)
    poetry add 'uvicorn[standard]'
    
    # Remove a dependency
    poetry remove requests

    💡 Always commit both pyproject.toml and poetry.lock so teammates get identical installs

    📌 Use --group dev for linters, formatters, and test tools to keep prod images lean

    Install & Update

    bash
    # Install all dependencies from poetry.lock (reproducible)
    poetry install
    
    # Install without dev dependencies (CI / production)
    poetry install --without dev
    
    # Update all dependencies to their latest allowed versions
    poetry update
    
    # Update a single package
    poetry update requests
    
    # Show what would change without applying it
    poetry update --dry-run

    💡 poetry install uses poetry.lock for exact versions — never poetry update in prod

    📌 --without dev is the standard flag for lean CI / Docker production stages

    Show & Check Dependencies

    bash
    # List all installed packages with versions
    poetry show
    
    # Show the full dependency tree
    poetry show --tree
    
    # Show outdated packages
    poetry show --outdated
    
    # Check pyproject.toml for validity issues
    poetry check

    💡 poetry show --tree helps spot transitive dependency conflicts before they hit CI

    Virtual Environments

    1 topic

    Manage Virtual Envs

    bash
    # Run a command inside the project's virtualenv without activating it
    poetry run python script.py
    poetry run pytest
    
    # Spawn a shell with the virtualenv activated
    poetry shell
    
    # List virtualenvs associated with the project
    poetry env list
    
    # Switch to a specific Python version (must already be installed)
    poetry env use python3.12
    
    # Delete the current virtualenv (useful to rebuild from scratch)
    poetry env remove python3.12

    💡 Prefer poetry run <cmd> in scripts and CI over activating the shell — it's explicit and composable

    📌 If you set virtualenvs.in-project true the venv lives at .venv/ — add it to .gitignore

    Scripts & Entrypoints

    1 topic

    Define & Run Scripts

    toml
    # In pyproject.toml — define CLI entrypoints and shortcut scripts
    [tool.poetry.scripts]
    # Installs a `my-cli` command pointing to the start() function
    my-cli = "my_project.cli:start"
    
    [tool.poetry.scripts.dev]
    # Custom shortcut: `poetry run dev` triggers this shell command
    # (requires poethepoet plugin for task runner support)
    server = "uvicorn my_project.main:app --reload"

    💡 After poetry install, the entrypoint binary is placed in the venv's bin/ and available via poetry run

    📌 For more complex task running use the poethepoet plugin (poe the poet)

    Building & Publishing

    2 topics

    Build a Distribution

    bash
    # Build both a source distribution (.tar.gz) and a wheel (.whl)
    poetry build
    
    # Build only a wheel (faster, preferred for most packages)
    poetry build --format wheel
    
    # Artifacts land in dist/
    ls dist/

    💡 Wheels install faster than sdists — build both so pip can choose

    📌 Bump the version in pyproject.toml with poetry version patch|minor|major before building

    Publish to PyPI

    bash
    # Bump the version (patch = 0.1.0 → 0.1.1, minor → 0.2.0, major → 1.0.0)
    poetry version patch
    
    # Build fresh artifacts
    poetry build
    
    # Publish to PyPI (uses token set via `poetry config pypi-token.pypi`)
    poetry publish
    
    # Build + publish in one step
    poetry publish --build
    
    # Publish to a private/custom repository
    poetry publish --repository my-private-repo

    💡 Store your PyPI token with poetry config pypi-token.pypi <token> — never hard-code it

    📌 Use TestPyPI first: poetry publish --repository testpypi to catch issues before going live

    Related Articles

    Background reading and deeper explanations for this sheet.

    Build an Agentic App with FastAPI and Azure AI Foundry: A Practical Beginner-to-Pro Guide

    Learn how to design, build, and deploy an agentic application using FastAPI and Azure AI Foundry with practical, real-world examples. This guide walks you from architecture and setup to tool-calling, memory, observability, and production deployment patterns.

    keyword overlap

    How to Host Small Models with Ollama on Your Local Machine (Beginner to Production)

    Want private, fast AI on your laptop without cloud costs? This practical guide shows you exactly how to host small models using Ollama in local machine setups, from installation to API integration and troubleshooting. You’ll get real commands, real-world examples, and a production-minded workflow you can use today.

    keyword overlap

    Create a Generative Email Triage App with FastAPI, Postgres, LangGraph, and Azure Foundry

    Learn how to build a production-ready generative email triage app using FastAPI, Postgres, LangGraph, and Azure Foundry. This guide walks you through architecture, data modeling, workflow orchestration, and practical implementation details with real-world examples. By the end, you’ll have a beginner-friendly but deep blueprint you can run, extend, and deploy.

    keyword overlap

    Event-Driven Agents with Kafka Streams: A Practical Guide for Building Real-Time AI Workflows

    Event-driven agents let you move from slow, request/response automation to responsive systems that react in milliseconds to real-world signals. In this hands-on guide, you’ll learn how to design, build, and operate agentic workflows using Apache Kafka and Kafka Streams, with practical patterns, code snippets, and production-ready advice.

    keyword overlap