Python is an easy-to-use with the ability to create machine learning programs. One of its key aspects is its use of dependencies and open-source libraries. Dependencies refer to the external packages installed separately from the original python package. In this article, we will learn about Poetry in Python and why use it at all.
What is Poetry in Python?
Dependencies are imported into the python program using the ‘import’ keyword. These libraries have pre-written code that can be easily integrated into the existing code of the user. Examples include pandas, numpy, matplotlib, and sklearn package.
For such a widely used programming language, it is crucial to have a fast and reliable method of downloading and managing these outside dependencies and packages. These packages are traditionally downloaded or installed into the user’s system using the keywords ‘pip’ or ‘pipenv’ at the command prompt of the system.
Poetry is a modern dependency management and packaging tool for python. It offers a more user-friendly approach and it simplifies the process of managing dependencies and projects.
Poetry in python does not refer to the literary ‘writing poems’ type of art, but it is merely a name given to the tool that was created for managing packages.
The streamlined dependency specification of Poetry is one of its primary characteristics. Poetry employs a straightforward pyproject.toml file to handle packages and projects as opposed to dealing with complicated configuration files or individually defining each package and version.
This file contains a list of every package and library that your project requires, along with the exact versions required. Your project will have a consistent and repeatable set of dependencies thanks to Poetry's automatic generation of a lock file that lists all the packages and their exact versions.
Why use Poetry Python?
Not to say there’s something wrong with using either ‘pip’ or ‘pipenv’, but Poetry was created to deal with certain issues in both, or at least issues in the older versions of both. These are mentioned below, along with how Poetry aims to deal with those issues.
- Dependency Management: Pip requires some manual configuration to use at maximum efficiency. Poetry aims to dispel this and be more user-friendly so that the common tasks will be more simple.
- Better Resolver: Poetry has a more advanced dependency resolution, so it can handle conflicts more effectively and does not lead to issues in version pinning.
- Virtual Environment: Poetry uses a built-in virtual environment which is easier to configure. Whereas, both pip and pipenv relied on the ‘venv’ module which could be slightly cumbersome to set up.
- Actively Updated: Since it’s newer, Poetry is actively developed and managed, with frequent bug fixes and updates with more functionalities.
- Packaging: It’s easier to manage packages that have been created using Poetry's PyPI. It contains several built-in commands that make it much easier to handle packages, so it saves time in the long run.
Is Poetry better than pipenv?
Pipenv is an older or more traditional dependency management tool of Python. It is very similar to Poetry as it does the same task, but there are certain key differences, which we go into detail about.
First, Pipenv uses a Pipfile to manage dependencies, rather than the pyproject.toml file used by Poetry. The Pipfile is similar to the requirements.txt file used by pip, but with some added features like support for dev dependencies and environment-specific packages. These are very useful, but they add to the complexity and result in more files to deal with in the long run.
Pipenv and poetry also differ in that Pipenv depends on the system-provided Python environment, while Poetry creates and manages a virtual environment for the project by default. Poetry does this automatically. It helps in isolating packages and avoiding version conflicts in the python files and projects.
At last, poetry has a more powerful and user-friendly command-line interface that makes it simpler to carry out routine chores like installing and upgrading packages. It is easier to understand what is happening while running commands because of the user-friendly interface's clear output.
With its increased usage recently, Python has started also to increase the number of tools and libraries used in those very same complex programs.
As a newer dependency management tool, with more modern functionalities than the older pip and pipenv tools, Poetry makes it easier to create manage and maintain a simple, isolated, and efficient codebase. Now you know why to use Poetry in Python and why it is better than the others. Happy Learning :)