Poetry

Poetry is a dependency management and packaging tool for Python projects. It simplifies project setup, dependency resolution, and packaging, making it easier to manage and distribute Python applications.

Install

Install the poetry-plugin-export to export dependencies into a standard requirements.txt file.

poetry self update
poetry self add poetry-plugin-export
poetry config warnings.export false

Configure

On MacOS, Poetry's config.toml and auth.toml are located at ~/Library/Application Support/pypoetry

Run

Install commands

Install project dependencies, including dev dependencies.

poetry install 

To install any other dependency group, specify it.

poetry install --with other-group

Install all dependency groups.

poetry install --all-extras

Other commands

Poetry supports source distribution and wheels.

poetry build

Publish to public or private repositories.

poetry publish

Show the dependency tree.

poetry show --tree

Compare and contrast your dependencies with the latest packages.

poetry show --latest

Tips

pyenv

Poetry can become "confused" when switching between different pyenv versions. You may have to reinstall it with the activated version of Python.

curl -sSL https://install.python-poetry.org/ | python3.11 - 

requirements.txt

Install poetry-plugin-export as a plugin.

poetry self add poetry-plugin-export

Remove its warning.

poetry config warnings.export false

You can then generate a traditional requirements file from a Poetry TOML file. Here is a Makefile to do so.

REQUIREMENTS = requirements.txt

# Export the requirements with no environment markers.
$(REQUIREMENTS):
	@poetry export --without-hashes -f $(REQUIREMENTS) --output $(REQUIREMENTS)
	@sed -i '' 's/[[:space:]]*;.*//' $(REQUIREMENTS)

Timeouts

If Poetry times out when fetching packages, try extending its timeout.

export POETRY_HTTP_TIMEOUT=240

Resources

Poetry & VS Code

Poetry
Interactive graph
On this page
Install
Configure
Run
Install commands
Other commands
Tips
pyenv
requirements.txt
Timeouts
Resources
Poetry & VS Code