Python Poetry Cheat Sheet

By Rex Resurreccion Dec 12, 2020
Python Poetry Cheat Sheet

This Python Poetry cheat sheet is intended to be used as a quick reference to Poetry commands. I will also go over here the installation procedure in MacOS and Linux systems.

Python-Poetry Installation

This method of installation has been tested on both MacOS and Linux.

Currently, the default Python version in most MacOS and Linux system is 2.7. And the command will install the version of Poetry that is compatible with your Python.

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

If you have multiple Python version installed in your system, you can also explicitly specify this in the installation process.

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3

Poetry Adding & Removing Package [dev-]dependencies

When adding packages, always make sure that you have activated first the virtual environment (venv) to use the current Python version of your Poetry.

poetry add "Flask==1.1.2"

Adding package in dev-dependencies.

poetry add "Flask==1.1.2" --dev

Add local dependency by specifying the library path.

poetry add "/path/to/locallib"

At the same time you can remove a package from dependencies.

poetry remove Flask

Poetry Create .toml And Lock File

By initializing a new Poetry project, this will generate a file pyproject.toml interactively. The file will have all your package dependencies. If you are familiar with pip package manager then this is similar to that requirements.txt file.

poetry init

Relatively you can start a Poetry project by creating a new folder.

poetry new my-project

When Poetry has finished installing, it writes all of the packages and the exact versions of them that it downloaded to the poetry.lock file, locking the project to those specific versions. This lock file should also be included in your project repo so that everyone working on the project is locked to the same versions of the dependencies.

poetry lock

Poetry Manage Package Dependencies

Update all poetry packages that are defined in pyproject.toml.

poetry update

Alternatively, you can update individual packages by specifying the name.

poetry update Flask

Show the list of all packages installed with description.

poetry show

Show information about a specific package.

poetry show Flask

Poetry Add, Remove, List & Source Virtual Environment

For Poetry to identify that the folder is indeed a Poetry project, it has to have a pyproject.toml file.

Create a new virtual environment in the current project.

poetry env use $(which python3)

Find the list of virtual environments including its full path.

poetry env list --full-path

Remove a virtual environment. The last part of the command is the name of the virtual environment.

poetry env remove project-edtXRBsn-py3.7

Get the path to the current active Python interpreter.

poetry run which python3

Activating the current virtual environment.

source /path/to/python3.7/bin/activate

Take note, this is also similar to manually type the Python path.

source $(dirname $(poetry run which python3))/activate

Poetry Creating A Package & Versioning

Create a package in wheel format.

poetry build --format wheel

Update the alpha version of the next release number.

poetry version prerelease

Update the patch version of the next release number. Check out more version examples.

poetry version patch

Poetry Nested Project

Installing all dependencies, but not the project itself. Use the --no-root flag when installing packages inside each application.

poetry install --no-root

Install Poetry Dependencies For A Release

Once you are ready to package and release your application, Poetry has a way to install all dependencies excluding the ones for Development.

poetry install --no-dev

In automated deployment you will need to disable any interactive questions that could keep the installation into a pause.

poetry install -n

If this Python Poetry Cheat Sheet has been helpful, you might also like Docker command cheat sheet.

© YippeeCode.com 2020