Python has become one of the most popular programming languages due to its simplicity and versatility. With its extensive library ecosystem, developers can easily build complex applications by leveraging existing code. However, managing dependencies and packaging projects can be challenging, especially when dealing with multiple libraries and versions. In this article, we will learn about Poetry in Python and discover how it can make our development workflow more efficient and hassle-free.
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 replaces the need for multiple files like setup.py, requirements.txt, setup.cfg, and Pipfile, making dependency management and package building more straightforward.
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. Let's see some features of Python Poetry:
- 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.
How to Use Python Poetry?
To start using Python Poetry, you first need to install it on your system.
Installation of Python Poetry
Poetry supports various operating systems, including Linux, macOS, and Windows. You can install Poetry by following the official installation instructions provided on the Poetry website.
Once installed, you can verify the installation by running the poetry --version command in your terminal. If the installation was successful, you should see the version number displayed.
Creating a New Project with Poetry
To create a new project, open your terminal and navigate to the directory where you want to create your project. Then, run the following command:
poetry new myproject
Poetry will generate a new project directory with the specified name and populate it with the necessary files and folders. The project structure created by Poetry will include the following files and folders:
- pyproject.toml: The project configuration file containing metadata and dependencies.
- README.rst: A README file where you can provide information about your project.
- __init__.py: An empty Python file representing the main package.
With the project structure in place, you can start adding and managing dependencies using Poetry.
Managing Dependencies with Poetry
Managing dependencies is a critical aspect of any Python project. Poetry simplifies this process by providing a single file, pyproject.toml to manage all project dependencies.
To add a new dependency to your project, run the following command:
poetry add package_name
Poetry will fetch the latest version of requests and add it to the pyproject.toml file, along with its dependencies, if any.
To install all project dependencies, run the following command:
poetry install
Building Packages with Poetry
Building packages is an essential step when distributing your Python projects. Poetry simplifies this process by providing built-in commands to build distributable packages in different formats.
To build your project, run the following command:
poetry build
Poetry will create a distributable package in both source distribution (sdist) and wheel distribution (bdist_wheel) formats. These packages can be easily shared and installed on other systems. The built packages will be stored in the dist/ directory within your project. You can find the generated files with the .tar.gz and .whl extensions.
Publishing Packages with Poetry
Publishing your Python packages allows other developers to easily install and use them in their projects. Poetry provides commands to publish your packages to the Python Package Index (PyPI) or private repositories.
Before publishing, you need to create an account on PyPI or set up a private repository. Once you have your credentials, you can use the following command to publish your package:
poetry publish
Poetry will prompt you to enter your PyPI or private repository credentials. After authentication, it will upload your package to the designated repository, making it available for others to install.
Managing Virtual Environments with Poetry
Python Poetry automatically creates a virtual environment for each project, isolating its dependencies from the system's Python installation. This virtual environment ensures that the project runs with the correct dependencies, regardless of the system's Python version or installed packages.
To activate the virtual environment for your project, run the following command:
poetry shell
This will activate the virtual environment and provide you with a new shell session within the project's context. Any subsequent Python commands or installations will take place within this virtual environment.
To deactivate the virtual environment and return to your system's default environment, simply type exit or close the shell session.
Poetry and Dependency Locking
Dependency locking is a crucial aspect of Python package management. It ensures that your project uses the exact versions of dependencies that were tested and verified during development.
Python Poetry automatically generates a poetry.lock file when you install or update dependencies. This file contains the exact versions of all installed packages, along with their dependencies and hashes. By committing the poetry.lock file to your version control system, you ensure that all project contributors use the same package versions.
When running the poetry install command, Poetry will read the poetry.lock file and install the exact versions of the dependencies listed there. This guarantees consistent behavior across different development environments.
Integrating Additional Tools with Poetry
Python Poetry integrates seamlessly with various development tools, allowing you to enhance your development workflow. Some notable tools that work well with Poetry include:
- Black: A popular code formatter that enforces a consistent coding style. You can integrate Black into your Poetry workflow to automatically format your code.
- Pytest: A powerful testing framework for Python. Poetry integrates smoothly with Pytest, allowing you to easily run tests for your project.
- Flake8: A tool for enforcing code style and detecting potential errors in your Python code. You can incorporate Flake8 into your Poetry workflow to maintain code quality.
To use these tools with Poetry, you can add them as development dependencies in your pyproject.toml file. For example, to add Pytest as a development dependency, run the following command:
poetry add --dev pytest
This will add Pytest to the [tool.poetry.dev-dependencies] section of your pyproject.toml file. You can then run Pytest tests using the poetry run pytest command.
Similarly, you can add other development tools to streamline your development process.
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.
Conclusion
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. With its intuitive interface and simplified configuration, Poetry streamlines the development workflow and ensures consistent dependency versions across different environments.