Skip to main content

Poetry

dependency management

Links

Poetry is a Python tool that facilitates dependency management and packaging. It allows developers to declare the libraries their project depends on and manages (installs/updates) them efficiently. Poetry offers a lockfile to ensure repeatable installs and can build your project for distribution, streamlining the development workflow.


Usage Examples​

Creating a New Project​

Create a new project directory with a basic structure:

poetry new my-project

Adding Dependencies​

Add a package as a dependency to your project:

poetry add requests

Add a development dependency (e.g., for testing or linters):

poetry add pytest --dev

Installing Dependencies​

Install all the dependencies specified in pyproject.toml:

poetry install

Running Scripts​

Run a script or command in the context of the project’s virtual environment:

poetry run python my_project/main.py

Managing the Virtual Environment​

Activate the virtual environment:

poetry shell

Deactivate the virtual environment by simply exiting the shell session.

Updating Dependencies​

Update specific dependencies:

poetry update requests

Update all dependencies to the latest compatible versions:

poetry update

Building and Publishing​

Build the project for distribution:

poetry build

Publish the project to a package index (e.g., PyPI):

poetry publish --username <username> --password <password>

Listing Dependencies​

Show the installed dependencies and their versions:

poetry show --tree