Skip to content

Managing Projects with Poetry

Python Tutorial — Unit 28

Installing and configuring Poetry

Installing Poetry

curl -sSL https://install.python-poetry.org | python -

Configuring Poetry

Configuring poetry settings. Example set default python version:

poetry config virtualenvs.python /usr/bin/python3

Understanding projecto.tomlfile

Here's a brief overview of its main sections:

  • [tool.poetry]: This section contains metadata about the project, such as the name, version, description, authors, and more.
  • [tool.poetry.dependencies]: This section lists the project's dependencies. You can specify the required packages and their versions here.
  • [tool.poetry.dev-dependencies]: This section lists the development dependencies, which are only needed for development and testing.
  • [build-system]: This section defines the build system used by the project, typically specifying Poetry itself.

Adding and Managing Dependencies

Add/remove dependencies:

poetry add requests
poetry remove requests

update dependencies:

poetry update requests

update all packages:

poetry update

Managing the Project

  • poetry install: Installs the project's dependencies as specified in the poetry.lock file.

  • poetry build: Builds the project package.

  • poetry publish: Publishes the package to a package repository like PyPI.

  • poetry run: Executes commands in the virtual environment where the project's dependencies are installed.

Manging Virtual Environments with Poetry

Check env settings:

poetry env info

Activating / Deactivating Virtual Envs

poetry shell

to deactivate: exit

Managing multiple virtual environments

List all environments:

poetry env list

remove:

poetry env remove python-version

Using poetry with Docker

Integrating Python Poetry with Docker (stackoverflow)