Python Poetry vs PIP

Introduction

Python Poetry vs PIP
Python Poetry vs PIP

We will go over the differences between Python Poetry vs PIP.

Did you know that Poetry uses PIP under the hood but the abstraction makes it a different system that’s comparable?

I’ve always used PIP in my Python projects but the last few months I was introduced to Poetry and I’ve been liking it a lot. I think it has it’s place so I wanted to write about it and see how it compares with PIP and whether or not it’s a good fit for your project.

We will break down this in the following sections:

  • What is the Difference between Poetry and PIP
  • Is Poetry A Replacement for PIP
  • Detailed Comparison PIP vs Poetry

I have used both PIP and Poetry successfully in various projects and they both work like a charm. I believe both have a place in my heart but which one do I use the most and why? I will be breaking down this and you can make your own mind up.

We will go point by point on how both compare in various tasks and what is the advantage of each of them.

Quick Comparisson

If you don’t want to spend a lot of time going through the differences between the two, I made a quick comparison table for you to go through and see the differences between the two and hopefully make a choice for yourself.

For more detailed information and how these two compare you will need to keep reading on the sections below as I answer very popular questions and I go into depth on their differences so you can make a more educated choice for your next project.

PIPPoetry
InstallationEasyEasy
Ease Of UseAverageEasy
Package SupportExcellentExcellent
Dependency ResolutionGoodExcellent
Package BuildingAverageEasy
SecurityGoodAverage
SpeedFastSlow
IntegrationGoodAverage
FeaturesBadGood
PopularityGoodBad

What is The Difference Between Poetry and PIP

Pip is considered by most the standard package manager that Python comes with. However this is not necessarily the case as a lot of you know that you need to install it separately.

Yes it’s bundled in with a lot of installers and operating systems but this does not make it the standard. Going forward PIP lets you manage in an easy way what is installed in your system and allows you to quickly add, remove and list packages.

Poetry is based on PIP and basically extends it’s feature set. It offers an easier way to do what PIP does and enhances the features and offering things on top of it such as deploying your own packages effortlessly in Py. Having said that both solve the same problem but in different ways and one builds upon the other one.

Is Poetry A Replacement for PIP

As we discussed earlier Poetry can be a replacement for PIP however it does rely on it behind the scenes. So if you take it as a strict definition meaning that you would have install Poetry without PIP this will not be possible.

If you loosen up a bit your definition of replacement and mean using it at a high level without having to use PIP (aside from the initial install) then sure you can say that poetry can replace PIP for your day to day use.

Now that we clarified this I would like to stress out the fact that based on your viewpoint this can go either way so please don’t blame me for which definition I give. I prefer to state the facts and let you decide.

Detailed Comparison PIP vs Poetry

Now that we went over some definitions lets start diving into the deep waters and break down each feature to help you decide which solution is better for you. Once again keep in mind that you can think of Poetry as a superset of PIP when it comes to how it works behind the scenes. However once you get into how it’s being used then the situation changes and this is what we will try to analyze here.

Installation / Update

PIP vs Poetry - Installation
PIP vs Poetry – Installation

The first section we will be covering is the installation and update process of the package itself. Both of these packages provide very simple ways of doing it but let’s go more into the specifics to fully understand this process.

For PIP Python has a module that supports it out of the box but you still need to install it in your system. The command below can accomplish this process.

$ python -m ensurepip --upgrade

For Poetry you need to go a bit outside your comfort zone and run an external curl command to get an installer script that’s written in Python. This is merely a wrapper to get the right pip package and install it on your system.

This can be accomplished using the following command.

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

When it comes to upgrading both systems fairly simple ways of doing so. The commands below show you to do this for PIP.

$ pip install --upgrade pip

As you can tell it’s fairly simple one-liner, similarly poetry has also a more intuitive one liner that lets you upgrade it’s command.

$ poetry self update

Since Poetry requires more dependencies to get installed on your system I’d say that PIP is slightly easier to work with and I would give it an edge in our case. Moving forward I’d say the winner here is PIP.

In the latest versions of poetry there has been some updates to make the whole process easier. However since you still need to go through the loops of executing more commands that under the scenes some rely on using curl since poetry is not pre-bundled in your system I believe PIP still holds an edge on being better when it comes in the overall installation process and compatibility with different Linux distributions.

Winner: PIP

Ease Of Use

PIP vs Poetry - Ease Of Use
PIP vs Poetry – Ease Of Use

Both packages are very easy to use. However as we can see from all the commands Poetry has an edge here since the command line interface is more intuitive. Furthermore some things in PIP like finalizing the versions of packages you have in your virtual environment require a series of commands. More particularly lets take a look and see how you can do this using a requirements text file.

$ pip freeze > requirements.txt

You can redirect the output of your freeze command when you finish working. You can also edit the requirements file but that’s a bit harder to do.

With poetry you have a nice package dependency file that allows you to work with versions similar to npm/yarn. If we take a sneak peak this looks like this.

[tool.poetry]
name = "unbiased-coder-project"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.9"
python-dotenv = "^0.20.0"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

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

The important line above is the section that shows the tool.poetry.dependencies. You can define all your packages there along with minor/major versions.

Since poetry has a much more advanced and automated way of doing this I would give this section to Poetry. Also you need to keep in mind that the PIP commands are kind of cryptic to remember and memorize. Take for example the freeze command, it’s difficult to understand that it just means list existing package versions. On the other hand Poetry has followed a more simplistic naming that’s very similar to other packages out there making it easy for the user to run the commands. At the end of the day with these tools it needs to be easy for you to remember them off hand as you use them a lot on your day to day tasks.

Winner: Poetry

Package Support

PIP vs Poetry - Package Support
PIP vs Poetry – Package Support

One of the most important aspects is package support. This is very intuitive in this case and we should be taking it very seriously. Below you can see how easy it is to add a package in your poetry setup which also automatically keeps things in a sandbox.

In the case below we are simply adding the python-dotenv package which also updates the package file we examined earlier for us without having to edit any files.

$ poetry add python-dotenv
Creating virtualenv unbiased-coder-project-q3DVRnTY-py3.9 in /Users/alex/Library/Caches/pypoetry/virtualenvs
Using version ^0.20.0 for python-dotenv

Updating dependencies
Resolving dependencies... (3.6s)

Writing lock file

Package operations: 9 installs, 0 updates, 0 removals

  • Installing pyparsing (3.0.9)
  • Installing attrs (21.4.0)
  • Installing more-itertools (8.13.0)
  • Installing packaging (21.3)
  • Installing pluggy (0.13.1)
  • Installing py (1.11.0)
  • Installing wcwidth (0.2.5)
  • Installing pytest (5.4.3)
  • Installing python-dotenv (0.20.0)

When it comes to PIP however the process is a bit more involved. You need to do the following steps to get the same effect:

  • Create a virtual environment where all the packages will be installed.
  • Activate the virtual environment so we can start using it
  • Install the packages inside the virtual environment
  • Save the packages that we just installed by updating our requirements file.

This process can be seen below.

$ virtualenv venv
created virtual environment CPython3.9.12.final.0-64 in 166ms
  creator CPython3Posix(dest=/Users/alex/ff/venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/alex/Library/Application Support/virtualenv)
    added seed packages: pip==22.0.4, setuptools==62.1.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
$ source venv/bin/activate
$ pip install python-dotenv
Collecting python-dotenv
  Using cached python_dotenv-0.20.0-py3-none-any.whl (17 kB)
Installing collected packages: python-dotenv
Successfully installed python-dotenv-0.20.0

It’s pretty obvious from the above commands that the winner here is Poetry. The process is a lot simpler and makes everything a breeze whereas with PIP you have to memorize a lot of commands do things manually. Additionally Poetry allows you to specify things in groups and makes it more convenient to keep things tidied up which is a huge differentiator to bundling.

Winner: Poetry

Dependency Resolution

PIP vs Poetry - Dependency Resolution
PIP vs Poetry – Dependency Resolution

Another important aspect of a Package manager is dependency resolution. This comes down to personal trial and error and it’s hard to prove with hard facts so I’ll just go ahead and tell you my experience after using both package systems over a long period of time.

I can say here that while Poetry can be extremely slow at some times it’s better at resolving package dependencies. Especially if you are using an odd mix of older package distributions it will resolve those for you and pull what’s appropriate.

In contrast with PIP while it does a very good job when you are using all the latest stuff for your packages it does not work very well when you go with older packages or a mix of some. Once you do a freeze this would be your golden bullet and set the seal. While this is ok it can be problematic if one of your packages requires a security update.

In that case you have to update the version to a later one and this is where most problems stem from. Once you do an update in one of your packages in your requirements file then resolving the rest of the dependencies which are set in stone in requirements can be a very big hassle. Poetry does not have this issue in fact it will do all the heavy lifting for you.

So based on your situation if you don’t really care on using older packages or freezing then both package managers are great, otherwise it’s safer to go with Poetry as it gives you more power and flexibility at the end of the day.

Winner: Poetry

Package Building

PIP vs Poetry - Package Building
PIP vs Poetry – Package Building

This is a feature that PIP simply does not have. With Poetry you can basically bundle and upload your package as is from the build directory that you initialized it in. Since there isn’t much comparison here to do with PIP as you need to do everything manually I’d say when it comes to package building and bundling your code Poetry is the winner without competition.

Winner: Poetry

Security

PIP vs Poetry - Security
PIP vs Poetry – Security

Security is a big aspect of your package manager as it maintains the integrity and reputation of your code from attackers. Since PIP comes with a lot less dependencies and generally speaking most people that use PIP tend to use the latest packages I think it has a slight edge over Poetry on being more secure.

The idea that Poetry has more code and does more things, also introduces more attack vectors in the code and adds space for more exploitation. Generally the less code and components the better for your security.

Since PIP shines in all of the above I can safely say that it offers a more secure and safe environment to work with.

Winner: PIP

Speed

PIP vs Poetry - Speed
PIP vs Poetry – Speed

When it comes to execution speed PIP is much better than Poetry. While Poetry is great when it has to do with package dependency resolution it’s extremely slow at doing so.

PIP on the other hand is almost always instant and I believe this is because of how the wheel packages are read and processed. If you do some timed runs in complex projects Poetry will be noticeably slower. However in small projects with a few dependencies you will not notice much of a difference at all and both operations will complete in the same amount of time.

Based on the above statement you can go with whatever is closer to you. I personally don’t think speed is an important factor like the earlier sections since it’s most of the times a one time deployment thing and if it takes a bit longer to do I don’t mind waiting.

Offcourse the situation changes in a more dynamic container like environment where everything is built and executed on the fly. Furthermore if you are using Lambdas in AWS or other server less platforms that do on demand build and deploy before they execute the code this may be an important factor for you. As over time this will build up and give you a hit in your initialization time along with the overall process of running the code.

Winner: PIP

Integration

PIP vs Poetry - Integration
PIP vs Poetry – Integration

When it comes to integration we will be talking how easy it is to add a package management system to a deployment code such as a third party system, more particularly I have in mind these:

  • Docker
  • AWS Lambda
  • Shell script
  • Deployment code such as Jenkins, Azure Devops, Gitlab etc

The most important thing is in all of those is how well it’s scriptable the code can be. To do this we need to examine a few examples of how we can do this from a shell script.

Let’s start by looking first at PIP.

$ source venv/bin/activate
$ python ./myscript.py

As you can see above PIP is very easily scriptable however it does require that you create and activate a virtual environment manually. We covered this earlier in the article and it’s a bit of a tedious task to do.

If we examine how this is done in Poetry we will see that it’s a lot simpler.

$ poetry run python myscript.py

Yes this is right Poetry basically lets you run things inside the sandbox it creates seamlessly. This is a big bonus as all it would require from you is to initialize and install your packages. If you are someone that has limited experience on writing code and using test cases then Poetry is the solution for you. It also simplifies the process for testing.

Lets see how easy it is to run pytest in your code within the sandbox.

$ poetry run pytest

Similar to above you simply just run the command and this takes care of all the testing and abstraction for you effortlessly. I would say here the judge has an easy choice for which solution is better.

Winner: Poetry

Features

PIP vs Poetry - Features
PIP vs Poetry – Features

When it comes to features as we explained earlier Poetry is a superset of PIP so it has much more things to offer. Keeping this in mind it’s a bit of an unfair comparison as this review is focused entirely on them being used as package managers.

Even though in that particular case I think Poetry has a much more feature rich set of how you manage and control your packages. So I think it’s a better overall solution than PIP. If you don’t need any of that flexible things it offers you can skip directly and use PIP as it’s pretty good enough to get the job done too.

Winner: Poetry

Popularity

PIP vs Poetry - Popularity
PIP vs Poetry – Popularity

Let’s close this final comparison on popularity. For this we will be using the help of google trends to see how well adopted each of those ecosystems are. To make it a bit more fair I’m going to limit this to the last 90 days as Poetry is a fairly newer system to PIP. Since PIP has been around since the start of time it will not be a fair comparison to backdate this popularity graph.

PIP vs Poetry - Trend
PIP vs Poetry – Trend

As you can see above PIP still wins by a long margin when it comes to popularity. I would dare say that Poetry is still fairly unknown to the world. One thing to note here is that I had to look for the term Python Poetry which may have reduced a bit the chances of what the user is searching for.

To counter this I tried a variation of terms specific to poetry such as installing and others but I found similar results to the graph shown above. Since PIP is much more popular for now I’d say the community is bigger and there will be better support if you are looking for a solution.

This does not discount in any way the Poetry project which I believe does a good job at fixing bugs and supporting the platform. In this case PIP is the clear winner and comes on top in the comparison.

Winner: PIP

Conclusion

We were able to successfully go over Python Poetry vs PIP, hopefully I answered any questions you may have had and helped you get started on your quest of selecting the right Python Package Management framework for your project.

If you found this useful and you think it may have helped you please drop me a cheer below I would appreciate it.

If you have any questions, comments please post them below or send me a note on my twitter. I check periodically and try to answer them in the priority they come in. Also if you have any corrections please do let me know and I’ll update the article with new updates or mistakes I did.

Would you consider moving from PIP to Poetry?

I personally think both have their place in the right project. It really depends on what you want to accomplish and how you want to go about it. Even though I love poetry I will still be using PIP for most of my projects as I find I do not need most of it’s features.

If you would like to visit the official Poetry documentation here.

If you would like to visit the official PIP documentation you can find it here.

If you would like to find more articles related to Python:

Leave a Comment

Your email address will not be published. Required fields are marked *