Poetry

Poetry — это инструмент для управления зависимостями и упаковки в Python. Он позволяет вам объявить библиотеки, от которых зависит ваш проект, и он будет управлять (устанавливать/обновлять) их для вас. Poetry предлагает файл блокировки для обеспечения повторных установок и может создать ваш проект для распространения.

# --- установка
curl -sSL https://install.python-poetry.org | python3 -
# --- обновление
poetry self update
# --- обновление до конкретной версии
poetry self update 1.2.0
# --- полностью удалить poetry из системы
curl -sSL https://install.python-poetry.org | python3 - --uninstall
# --- или
curl -sSL https://install.python-poetry.org | POETRY_UNINSTALL=1
python3 -

Новый проект

# --- новая директория
poetry new test_project
poetry-demo
├── pyproject.toml
├── README.md
├── poetry_demo

└── __init__.py
└── tests
└── __init__.py

# --- существующая директория
cd test_project
poetry init

Текущие настройки poetry

poetry config --list
cache-dir = "/home/user/.cache/pypoetry"
experimental.system-git-client = false
installer.max-workers = null
installer.modern-installation = true
installer.no-binary = null
installer.parallel = true
virtualenvs.create = true

Зависимости

# pyproject.toml
[tool.poetry.dependencies]
pendulum = "^2.1"

или

poetry add pendulum@^2.1

Управление зависимостями

# --- установка
poetry install
# --- обновление
poetry update
# --- обновление заданных пакетов
poetry update icecream pygame
# --- удалить пакет из проекта
poetry remove pygame
# --- отобразить зависимости пакета
poetry show requests

Посмотреть все установленные библиотеки и зависимости

poetry show --tree

rettytable 3.9.0 A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
└── wcwidth *
pymongo 4.5.0 Python driver for MongoDB <http://www.mongodb.org>
└── dnspython >=1.16.0,<3.0.0
pytest 7.4.2 pytest: simple powerful testing with Python
├── colorama *
├── iniconfig *
├── packaging *
└── pluggy >=0.12,<2.0

Посмотреть текущие версии библиотек и сравнить версии с новыми

poetry show --latest
asttokens         2.4.0  2.4.1  Annotate AST trees with source code positions
backcall          0.2.0  0.2.0  Specifications for callback functions passed in to an API
click             8.1.7  8.1.7  Composable command line interface toolkit
decorator         5.1.1  5.1.1  Decorators for Humans
dnspython         2.4.2  2.4.2  DNS toolkit

Виртуальное окружение

# --- активация
poetry shell

# --- запустить скрипт в окружении
poetry run python your_script.py

# --- отключить автоматическое создание виртуального окружения
poetry config virtualenvs.create false

# --- информация об окружении
poetry env info
Virtualenv
Python:         3.11.4
Implementation: CPython
Path:           /home/user/.cache/pypoetry/virtualenvs/altermongo-hkSQzIq4-py3.11
Executable:     /home/user/.cache/pypoetry/virtualenvs/altermongo-hkSQzIq4-py3.11/bin/python
Valid:          True

System
Platform:   linux
OS:         posix
Python:     3.11.4
Path:       /usr
Executable: /usr/bin/python3.11

# --- Создать окружение с нужной версией Python, которая лежит в PATH:
poetry env use python3.7

# --- Узнать путь к папке с окружением
poetry env info --path

# --- Удалить виртуальную среду
poetry env remove /full/path/to/python
poetry env remove python3.7
poetry env remove 3.7
poetry env remove test-O3eWbxRl-py3.7

# --- Удалить несколько виртуальных окружений за раз
poetry env remove python3.6 python3.7 python3.8