Poetry: How to manage Package Dependencies in Python

Emmanuel bolarinwa
Data Epic
Published in
3 min readDec 4, 2023

Keeping track of dependencies related to a Python project can be pretty tricky, especially when doing this manually. This is where package managers come in. Package Managers help you store only the necessary libraries needed to run your project. It ensures reproducibility. There are several package managers like conda, and pipenv that exist to keep track of dependencies related to your project. In this article, we will explore using Poetry for your dependency management in Python.

Introducing Poetry

Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Installation

Poetry supports Python 3.8+ and works on Linux, Windows, and macOS. To get started with Poetry, you can install it using curl on Linux. If you don't have curl installed, you can check out curl installation

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

You can also check the official page for more installation instructions for macOS and Windows. Once the installation is complete, you can confirm that the installation was successful by running the following command.

poetry --version

You can also view Poetry configuration by running the following command.

poetry config  --list

Project Setup

To initialize your project folder using poetry, run the following command

poetry new new_project

If your project folder already exists, you can change the directory into your project folder, then run the following command to initialize poetry in your project.

poetry init

Poetry will display a few prompts. This response will be used to create and populate a project.toml file in your project directory. It also allows you to define your dependencies independently.

Your project.toml file should be like this

Virtual Environment

Poetry also handles virtual environments as this is very important in ensuring that your project dependencies are isolated from other packages on your system.

I advise configuring poetry to store the project virtual environment in the project folder. It makes it easier to interact with your virtual environment. You can do that by running the following command.

poetry config virtualenvs.in-project true

You can use poetry to spin a virtual environment from the project.toml file using shell. It is easier to activate the environment using shell. To spin the virtual environment, run the following command

poetry shell

Alternatively, you can manually activate the virtual environment using the following command

source {path_to_venv}/bin/activate

To get the path to your virtual environment, run

poetry env info --path

To learn more about activating a virtual environment, you can check Poetry’s virtual environment documentation here

Package Installation

To install a package into your virtual environment using Poetry, run the following command

poetry add <package-name>

This automatically installs the package into your environment. It also creates a poetry.lock file similar to pipfile.lock This will ensure that anyone installing the project gets the same version of libraries. You can also install multiple packages by using the following syntax

poetry add <package-name-1> <package-name-2> <package-name-3>

If you have a requirements.txt file, then you can run

poetry add $( cat requirements.txt )

To view all the installed dependencies

poetry show

To remove a package from your environment, run the following command

poetry remove <package-name>

To deactivate the virtual environment in shell, run exit This will exit the environment. If you aren't using shell, run deactivate

Conclusion

Poetry is a modern tool that helps with package version management, and virtual environment creation. It automatically handles package resolution so you don’t have to worry about resolving conflicts. Poetry can also be used to build and publish your Python packages. You can check more about Poetry here.

If you find this article helpful, kindly give some clap and follow. Thanks!

--

--

Emmanuel bolarinwa
Data Epic

A data enthusiast with a strong background in CS. Always learning. I also play chess for fun