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

1
poetry new poetry-demo

In Pre-existing Directory

1
2
cd pre-existing-project
poetry init

Environments

Displaying the environment information

1
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.

1
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

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

Listing the environments associated with the project

1
poetry env list

Deleting the environments

1
2
3
4
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.

1
poetry env remove --all

Dependencies

Add new dependency

1
poetry add <package name>

Removing dependency

1
poetry remove <package name>

Installing dependencies

1
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.)

1
poetry update

Adding a dependency to a group

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

Installing group dependencies

without

1
poetry install --without test, docs

with

1
poetry install --with docs

only

1
poetry install --only main

Removing dependencies from a group

1
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.

1
poetry install --sync
1
2
3
poetry install --without dev --sync
poetry install --with docs --sync
poetry install --only dev

Export

1
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

1
poetry build

Publish

1
poetry publish

Configuration

You can find all the configuration options here.