Skip to content

Poetry Cheat Sheet for Developers

Introduction to Poetry

Poetry is a dependency management and packaging tool for Python that aims to simplify and enhance the development process. It offers features for managing dependencies, virtual environments, and building and publishing Python packages.

Virtual Environments with Poetry

Poetry manages virtual environments for your projects to ensure clean and isolated development environments.

Creating a Virtual Environment

poetry install

Activating the Virtual Environment

poetry shell

Alternatively you can source the environment settings in the current shell

source $(poetry env info --path)/bin/activate

for powershell users this would be

(& ((poetry env info --path) + "\Scripts\activate.ps1")

Deactivating the Virtual Environment

When using poetry shell

exit

When using the activate script

deactivate

Dependency Management

Poetry uses the pyproject.toml file to manage dependencies. Add new dependencies to this file and update existing ones as needed.

Adding a Dependency

poetry add package-name

Adding a Development Dependency

poetry add --dev package-name

Removing a Dependency

poetry remove package-name

Updating Dependencies

poetry update

Running Tasks with Poetry

Poetry provides a way to run scripts and commands without activating the virtual environment explicitly.

Running a Command

poetry run command-name

Running a Script

poetry run python script.py

Building and Publishing with Poetry

Poetry streamlines the process of building and publishing Python packages.

Building the Package

poetry build

Publishing the Package

poetry publish

Using Extras

Extras allow you to specify additional dependencies based on project requirements.

Installing with Extras

poetry install -E extras-name

for example

poetry install -E "askar bbs indy"

Managing Development Dependencies

Development dependencies are useful for tasks like testing, linting, and documentation generation.

Installing Development Dependencies

poetry install --dev

Additional Resources