Using Poetry Dependency Management tool in Python

Using Poetry Dependency Management tool in Python

Poetry is a modern tool for Python project dependency management and packaging. It helps to handle project dependencies and package configuration in an efficient and consistent manner. Here's a guide on how to use Poetry:

Installation

First, install Poetry. You can do this via a variety of methods, but the recommended way is to use the installer script:

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - 

or if you're using PowerShell on Windows:

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python - 

Creating a New Project

To create a new project with Poetry:

poetry new my_project 

This command creates a my_project directory with a basic project structure.

Adding Dependencies

To add a new dependency to your project:

cd my_project poetry add requests 

This command adds "requests" as a dependency and updates pyproject.toml and poetry.lock files accordingly.

Installing Dependencies

To install all dependencies defined in pyproject.toml:

poetry install 

Updating Dependencies

To update dependencies:

poetry update 

Specifying Python Versions

In pyproject.toml, you can specify the Python version(s) your project is compatible with. For example:

[tool.poetry.dependencies] python = "^3.8" 

This denotes that your project is compatible with Python 3.8 and above, but less than 4.0.

Building and Publishing Your Package

To build your project:

poetry build 

This command creates a distributable format of your package, like a wheel.

To publish your package to PyPI:

poetry publish 

Virtual Environments

Poetry automatically creates and manages a virtual environment for your project. You can enter this environment using:

poetry shell 

Or you can run commands within the environment without activating it:

poetry run python my_script.py 

Configurations and Settings

Poetry allows you to configure settings globally or per-project basis. For example, to change the repository for publishing packages:

poetry config repositories.myrepo https://myrepo.example.com 

Dependency Resolution

Poetry is known for its sophisticated dependency resolution algorithm, which ensures that you have a set of dependencies that all work together.

Locking Dependencies

The poetry.lock file is created to lock your project's dependencies (including transitive dependencies) to specific versions, ensuring consistency across installations.

Conclusion

Poetry is a powerful and user-friendly tool for Python dependency management and packaging. Its ability to manage virtual environments, along with its elegant handling of dependencies and package configuration, makes it a great choice for modern Python projects.


More Tags

order-of-execution iteration error-handling sql-query-store cron-task delphi-2007 spring-cloud-config regression contains date-arithmetic

More Programming Guides

Other Guides

More Programming Examples