Poetry

Yet another Python virtual environment & package manager!

Table of contents
  1. Installation
  2. Enable tab completion
  3. Using Poetry with a specific Python version
    1. Set Python binary
  4. Managing environments
    1. See all virtual envs associated with this directory/project
    2. Delete environments
  5. Basic usage
    1. Activate environment
    2. Deactivate environment
    3. Add dependencies
    4. Install dependencies
    5. Remove dependencies

Installation

Use Homebrew:

brew install poetry

Check installation via

poetry --version

Enable tab completion

You can enable poetry tab completion for various shells.

Check poetry help completions for other shells.

For Oh-My-Zsh:

mkdir $ZSH_CUSTOM/plugins/poetry
poetry completions zsh > $ZSH_CUSTOM/plugins/poetry/_poetry

Then go to ~/.zshrc and add the following plugin:

# ~/.zshrc
plugins(
  ...
  poetry
)

Using Poetry with a specific Python version

Have the Python version you want ready.

Always check that you do indeed have the version you want by which python

You can set the version you want

Now init Poetry in project to create a pyproject.toml file.

cd <project-dir>
poetry init

By default, Poetry uses the currently active Python.

Poetry virtual environments are created in ~/Library/Caches/pypoetry/virtualenvs.

Set Python binary

If you’d like to change a version after init, activate a new Python binary and do:

poetry env use `pyenv which python3`

Check that it is using the right binary by:

poetry env info

Managing environments

See all virtual envs associated with this directory/project

poetry env list

Delete environments

poetry env remove <proj-hash--py3.x>  ## Check exact name with poetry env list

Basic usage

Activate environment

poetry shell  # Creates a new child shell
# OR
source `poetry env info --path`/bin/activate  # Does not open a child shell

Deactivate environment

exit  # If in child shell
# OR
deactivate  # If activated with source <path>/bin/activate

Add dependencies

poetry add <package>
# OR
poetry add <package> --dev

Install dependencies

poetry install
# OR
poetry install --no-dev

Remove dependencies

poetry remove <package>
# OR
poetry remove <package> --dev

References: