-
Notifications
You must be signed in to change notification settings - Fork 18
Poetry Cheat Sheet
The purpose of this doc is to serve as a quick reference for some of the common workflow commands.
Most of the commands below can have -v|vv|vvv
added to them for increased verbosity when running the commands, the number of v
's added specifies the level,
- normal output
- verbose output
- for debug You can find all the juicy details in the official documentation.
Poetry's configurations can be set either from the shell, or by environment variables.
# List the current config
poetry config --list
# Update a config by key
poetry config experimental.new-installer <true|false>
When overriding a config with env vars, prefix the config key with POETRY_
and replace dots and dashes with underscores like so:
poetry config experimental.new-installer true
# Becomes
POETRY_EXPERIMENTAL_NEW_INSTALLER=true
poetry install
There are a number of ways you can include, exclude, or otherwise specify dependency groups when installing. Dependency groups are specified in pyproject.toml
with the following section specifier [tool.poetry.group.<desiredGroupName>.dependencies]
.
# Test dependencies only
poetry install --only test
# Exclude test dependencies
poetry install --without test
# Include test dependencies
poetry install --with test
Dependencies can be managed either via the shell or by editing pyproject.toml
directly. When editing pyproject.toml
directly, run poetry check
to validate the pyproject.toml
file before running poetry install
.
boto=="2.49.0"
# Becomes
boto = "2.49.0"
jinja2-cli[yaml]==0.8.2
# becomes
jinja2-cli = { extras = ["yaml"], version = "0.8.2" }
git+https://github.com/mitsuhiko/flask-sqlalchemy.git@500e732dd1b975a56ab06a46bd1a20a21e682262#egg=Flask-SQLAlchemy==2.3.2.dev20190108
# Becomes
Flask-SQLAlchemy = { git = "git+https://github.com/mitsuhiko/flask-sqlalchemy.git", rev = "500e732dd1b975a56ab06a46bd1a20a21e682262"}
git+https://github.com/cds-snc/[email protected]#egg=notifications-utils
# Becomes
notifications-utils = { git = "https://github.com/cds-snc/notifier-utils.git", tag = "50.1.0"}
poetry search <search term>
When adding a dependency and specifying a version, Version constraints can be used.
# Just the dependency name
poetry add pytest-mock-resources
# With a version
poetry add [email protected]
# Specify a dependency group target
poetry add [email protected] --group test
# Specify Extras (multiple can be specified separated by a space)
poetry add [email protected] --extras redis
# From a git url
poetry add git+https://github.com/mitsuhiko/flask-sqlalchemy.git
# From a git url with a revision
poetry add git+https://github.com/mitsuhiko/flask-sqlalchemy.git#500e732dd1b975a56ab06a46bd1a20a21e682262
# Just the dependency name
poetry remove pytest-mock-resources
# Specify a dependency group target
poetry remove [email protected] --group test
When changes are made to the lock file it should be committed along with any other work done.
# Validate the lock file
poetry lock --check
# Freeze the current requirements in the lock file
poetry lock
When pyproject.toml
is updated, either manually or via the CLI, the lock file needs to be refreshed.
poetry lock --no-update
When running poetry install
and the lock file is out of sync, you'll be notified of this.
When we need to package up a project, like notification-utils
, we no longer need to use setup.py
. Project metadata that was previously housed in setup.py
for this purpose is now found in pyproject.toml
:
[tool.poetry]
name = "notifications-utils"
version = "50.1.2"
description = "Shared python code for Notification - Provides logging utils etc."
authors = ["Canadian Digital Service"]
license = "MIT license"
readme = "README.md"
packages = []
# Create a source distribution
poetry build --format sdist
# Create a wheel
poetry build --format wheel
Poetry has built in virtual environment management. By default Poetry checks if it is being run from within a virtual environment and will utilize an existing environment. Otherwise Poetry will automatically create a virtual environment to isolate project dependencies from system dependencies.
# List all environments associated with he current project
poetry env list
# Get more detailed information about the currently activated env
poetry env info
# By path to python
poetry env use $(which python)
# By python version - if it exists in your path
poetry env use python3.10
poetry env use 3.10
# Explicitly use the system environment
poetry env use system
Environments can be removed in the same manner that they can be activated
# By path to python
poetry env use $(which python)
# By python version - if it exists in your path
poetry env use python3.10
poetry env use 3.10
# Explicitly use the system environment
poetry env use system
There are several useful configs for instructing poetry how to handle virtual environments:
# Enable or disable automatic venv creation
virtualenvs.create <true|false>
# Use the system environment, this is typically paired with virtualenvs.create false
virtualenvs.options.system-site-packages
# Specify the path for Poetry to use when creating virtual envs
virtualenvs.path <path>
# Instruct Poetry to create <project_root>/.venv and use it for creating virtual envs
virtualenvs.in-project <true|false>
# Use the currently installed python for creation of virtual environments
virtualenvs.prefer-active-python
In the case of CI pipelines and deployment environments it is useful to isolate Poetry from the system packages to avoid inadvertently removing or otherwise editing packages that either Poetry or the installed project depend on.
# Override Poetry's config to ensure it does not create venvs automatically
POETRY_VIRTUAL_ENVS_CREATE=false
# Set up your venv paths
POETRY_VENV=/poetry/belongs/here
PROJECT_VENV=/project/venv/belongs/here
# Add the virtual env paths to the PATH variable
PATH="#{PROJECT_VENV}/bin:${POETRY_VENV}/bin:$PATH"
# Create a venv to house poetry
python -m venv ${POETRY_VENV}
# Install poetry into it's venv
${POETRY_VENV}/bin/pip3 install poetry
# Create a venv for the project
python -m venv ${PROJECT_VENV}
# Activate the project venv and install dependencies
. ${PROJECT_VENV}/bin/activate
poetry install
For a more concrete example of how this plays out in practice see admin's Dockerfile.lambda.
poetry show --tree
This command will list the package's dependencies, as well as which other packages depend on it.
poetry show SQLAlchemy
# Get the info as a tree instead
poetry show SQLAlchemy --tree
poetry show --outdated