Python Poetry Cheat Sheet

Posted by Vivek Shukla on Oct 30, 2023 under Python

Poetry is an amazing tool for dependancy management in Python, it solves a lot of problems of pip. Here in this Poetry Cheat Sheet we have useful commands that we may need frequently.

Initializing

New Project

poetry new poetry-demo

In Pre-existing Directory

cd pre-existing-project
poetry init

Environments

Displaying the environment information

poetry env info

Using your own virtual environment

Keep virtualenv in project root

More on this here.

This will also let VS Code discover your porject’s virtual env.

poetry config virtualenvs.in-project true --local

External virtual environment management

Poetry will detect and respect an existing virtual environment that has been externally activated. This is a powerful mechanism that is intended to be an alternative to Poetry’s built-in, simplified environment management.

You can use pyenv to create external virtual environment.

Switching between environments

poetry env use /full/path/to/python
poetry env use 3.7
poetry env use system

Listing the environments associated with the project

poetry env list

Deleting the environments

poetry env remove /full/path/to/python
poetry env remove python3.7
poetry env remove 3.7
poetry env remove test-O3eWbxRl-py3.7

Use the --all option to delete all virtual environments at once.

poetry env remove --all

Dependencies

Add new dependency

poetry add <package name>

Removing dependency

poetry remove <package name>

Installing dependencies

poetry install

Updating dependencies to their latest versions

This will fetch the latest matching versions (according to your pyproject.toml file) and update the lock file with the new versions. (This is equivalent to deleting the poetry.lock file and running install again.)

poetry update

Adding a dependency to a group

poetry add pytest --group test
poetry add isort --group dev

Installing group dependencies

without

poetry install --without test, docs

with

poetry install --with docs

only

poetry install --only main

Removing dependencies from a group

poetry remove mkdocs --group docs

Synchronizing dependencies

Poetry supports what’s called dependency synchronization. Dependency synchronization ensures that the locked dependencies in the poetry.lock file are the only ones present in the environment, removing anything that’s not necessary.

poetry install --sync
poetry install --without dev --sync
poetry install --with docs --sync
poetry install --only dev

Export

poetry export -f requirements.txt --output requirements.txt
  • --format (-f): The format to export to (default: requirements.txt). Currently, only constraints.txt and requirements.txt are supported.

  • --output (-o): The name of the output file. If omitted, print to standard output.

  • --extras (-E): Extra sets of dependencies to include.

  • --without: The dependency groups to ignore.

  • --with: The optional dependency groups to include.

  • --only: The only dependency groups to include.

  • --without-hashes: Exclude hashes from the exported file.

  • --without-urls: Exclude source repository urls from the exported file.

  • --with-credentials: Include credentials for extra indices.

Packaging

Build

poetry build

Publish

poetry publish

Configuration

You can find all the configuration options here.

Built with  Svelte Starter Kit