Why you should care about Poetry
Tags: python, tutorial, beginners
Published: 2024-02-26 21:52:18

Let me be frank. Python dependency management with pip kinda is a pain.

I never found it too outstanding, but once I have experienced dependency management with ... (make sure the inner voice reading you this text takes on a loving tone for the next word) [Cargo](https://doc.rust-lang.org/cargo/) ... (back to normal) something had to change.

Before we talk about this change, let's first review what had to change.

## Forgetting dependencies

The following situation happens way to often to me.

I check out a repository from the company github, navigate to the file containing dependencies, let pip do its magic. Only to face an error message indicating a missing dependency once I try to run the code.

How the hell could that happen?

But all is not yet lost. I see what is missing, right? The solution is to simply pip install the missing library, right? Right?

Wrong!

See, what I just installed was the latest version of said dependency. But the code was written based on an older version of said dependency that saw several API breaking changes in the meantime.

Driven by anger and frustration, I open up the commit history of the repository to see who pushed the last changes ... only to find that it was me...

## Activating the virtual environment

Attentive readers might have noticed that some steps were missing in the last section.

Of course, you should create a virtual environment in which you install all requirements. Please, do not install everything globally, as that would be the direct path into dependency hell.

In the off-chance that you are unfamiliar with virtual environments, they are - as the name would imply - a dedicated Python environment, that neither interferes with your global environment nor with other virtual environments.

They are super useful and should be a trusted tool for all Python developers.

But their isolating super powers are worth nothing if you forget to actually activate it. Something that can quickly happen if you are in a hurry and especially if your shell does not indicate activated environments. Trust me, happened to all of us!

## How Poetry alleviates these issues

For installing dependencies, you can use the command `poetry add {name of dependency}`

Like pip, this installs the dependency. But unlike pip, it automatically installs it into a dedicated virtual environment for the current project. The goodness does not stop there. On top of it, it also adds the dependency to your pyproject.toml.

You can even [constraint the allowed versions of the dependency](https://python-poetry.org/docs/dependency-specification/) with powerful tools.

This can be achieved with 
`poetry add {name of dependency}@{constraint}`

In order to enable full repeatability of builds, it creates and maintains a lock file that contains the exact version numbers of your current configuration.

The section up to now already contains a hint at how it solve the second problem: By fully automating the handling of virtual environments. No longer do you need to activate it. You don't even have to remember to create it. Poetry does all of that for you.

Poetry keeps track which environment was used for which project and uses it whenever necessary.

The best thing about it, you can even have multiple environments for the same project. Gone are the days where you have to ask yourself: "Was env1 the environment with Python 3.9 or was it env2?"

Thus, in the end, all you need to do is type `poetry run python {file name}`.

There is also no need to create an environment for a repository you just checked out. Simply use `poetry install` and all the needed requirements are being installed into an environment that is also being created for you.

## Conclusion

I do not want to drag this one out any further as this article was intended to be an appetizer.

By now you are armed with some basic commands and hopefully some curiosity to try it out for yourself. So head on over to the [installation page](https://python-poetry.org/docs/#installation) and you can be the proud owner of your very own copy of poetry.

I hope you learned something in this article! Feedback is always appreciated and see you in the next one!