How to Use Poetry to Install Python Packages and Manage Dependencies

In this guide, you’ll see the full steps to use Poetry to install Python packages and manage dependencies.

Steps to use Poetry

Step 1: Create a new project

To start, create a new project (or navigate to an existing one).

For example, let’s say that you created a project called “check_poetry

Step 2: Install Poetry

Go to the terminal and install Poetry using this command:

pip install poetry

Step 3: Initialize a Poetry project

You can initialize a Poetry project using this command:

poetry init

This command will guide you through creating your pyproject.toml config file.

For example (you may fill the details based on your needs, or skip sections by pressing Enter):

Package name [check_poetry]:
Version [0.1.0]:
Description []:  This is my new project
Author [None, n to skip]: None
License []:
Compatible Python versions [^3.11]:

Would you like to define your main dependencies interactively? (yes/no) no

Would you like to define your development dependencies interactively? (yes/no) no

Do you confirm generation? (yes/no) [yes] yes

The pyproject.toml file will be created under your project’s directory. You may edit the above details in this toml file as needed.

Step 4: Add all your packages and their versions in the pyproject.toml file

Open the pyproject.toml file and add your packages under [tool.poetry.dependencies]

Here is an example, where we’ll add the “Pandas” and “Pytest” packages:

name = "check-poetry"
version = "0.1.0"
description = "This is my new project"
authors = ["None"]
readme = ""

python = "^3.11"
pandas = "^2.0.1"
pytest = "^7.3.0"

requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

Note that the “^” symbol means that Poetry will install the latest compatible version within the specified range.

Step 5: Install the packages and the dependencies

Use the following command to install all the packages you defined under the [tool.poetry.dependencies] section:

poetry install

Step 6 (optional): Check that the packages were added

You should be able to import and use the new packages without any errors.

For example:

import pandas as pd
import pytest

Step 7 (optional): Add new packages

If you decide to add a new package, you can then use the following command to add your package:

poetry add package_name

For example, add “matplotlib” as follows:

poetry add matplotlib

Check that the new package was added:

import matplotlib.pyplot as plt

Alternatively, you can add new packages under the [tool.poetry.dependencies] section in the pyproject.toml file:

python = "^3.11"
pandas = "^2.0.1"
pytest = "^7.3.0"
matplotlib = "^3.8.0"

And then type poetry update in the terminal:

poetry update

Step 8 (optional): Remove packages

You can remove a package using this command:

poetry remove package_name

Alternatively, you can remove packages under the [tool.poetry.dependencies] section in the pyproject.toml file, and then type poetry update in the terminal:

poetry update