Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python relies on the principle and usage of the management and build tool Poetry

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about the principle and usage of python dependency management and construction tool Poetry. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Dependency management has always played a vital role in programming languages, whether it's Python, JavaScript or Java or Go.

Although different programming languages have different dependency management tools, their goals are more or less the same. It can manage the project more easily and uniformly, and it is more convenient to migrate and deploy the project.

As a result, dependency package management tools such as maven, npm, and pip have emerged, but they are more special in dependency management than Java and JS,Python. Therefore, there have been a number of different management tools, each upgrade is to better do a better job of Python dependency package management tools.

The main particularity of Python in relying on package management comes from the need to consider not only the third-party packages since the project, but also the virtual environment. This is due to the essential differences between Python and programming languages such as Java and JS.

Java and JS are isolated by project. Whenever a project is developed, the dependency package is installed under the project project path. Each project is isolated so that there is no conflict between them, even if different projects use different versions of the same package.

Unlike Python, however, dependent packages cannot be isolated by engineering in Python.

When we use pip to install third-party packages between us, all dependencies are installed to site-packages in the installation directory.

Just imagine, if we develop Project A, we use TensorFlowv0.1. However, TensorFlowv0.2 is needed to develop B project. At this point, the dependency packages under site-packages will conflict, and the later installed dependency packages will uninstall the previously installed version and then install the new version of the dependency package.

Obviously, this is very unfriendly.

In order to solve this problem, virtual environment plays a vital role.

When developing different projects, you can create and activate different virtual environments. In this way, different projects will use parsers in different environments, and we can also install dependency packages to the site-packages paths of different virtual environments.

Therefore, dependency management in Python generally refers to dependency management + virtual environment.

In the past, two tools were often used for Python dependency management and virtual environments: pip and virtualenv.

Pip is used for dependent package management, and virtualenv is used for virtual environment management.

Although this solves the problem of environmental isolation between different projects, it also has obvious shortcomings:

Need to rely on two management tools at the same time

Cannot update requirements.txt dynamically

The more prominent problem is the second point.

During engineering development, a configuration file is needed to record dependency packages and environment parameters, such as maven's pom.xml and npm's package.json.

Requirements.txt is commonly used in Python.

This plain text format can only be used to record the names of dependent packages, not to record more environment parameter information like yaml, json, and xml text storage formats.

In addition, a more deadly point is that each time you export a dependent package, you need to manually execute the pip freeze > requirements.txt command. Obviously, this is very unreasonable. Just imagine, if you forget to perform this operation manually because you are busy, it will take a lot of effort to deploy to the production environment and find an error, which will cause a lot of loss.

Therefore, pipenv was born.

Pipenv

Pipenv is a Python dependency package management tool released by Kenneth Reitz, author of requests, flask and other well-known toolkits in 2017.

Pipenv can be seen as a combination of pip+virtualenv tools, which integrates pip dependency package management and virtualenv virtual environment management. In addition, Pipfile is used instead of the original requirements.txt in terms of relying on package records. Moreover, it can automatically record and update the record file, so that you no longer need to manually execute commands to update requirements.txt.

The emergence of pipenv does greatly reduce the complexity of Python dependency package management and improve the efficiency of project development and project migration.

Therefore, in recent years, the evaluation of pipenv has been very good, and there are many articles praising pipenv on major content platforms.

However, I have not become a loyal user of pipenv, and I will not choose pipenv to replace the original pip+virtualenv solution.

I believe that the authors who praise pipenv are not necessarily loyal users.

There is no denying that pipenv provides a good solution for Python dependency package management. However, it still has some fatal defects, which is more troublesome and more difficult than the original pip+virtualenv scheme.

Lock is slow.

Force update of irrelevant dependencies

The effect of dependency processing is poor.

Here is an example to explain point 3.

Install a package using pipenv

$pipenv install oslo.utils==1.4.0

At this time, an error will occur.

Could not find a version that matches pbricks, 0.7, 2.1.0, 0.6, > = 2.0.0

This error means that the right version of pbr cannot be found. Even if there is an appropriate version, pipenv will simply and rudely throw an error, unable to complete the processing and installation of the dependency package.

At this point, a better toolkit appears, which is poetry.

Poetry

Poetry is a third-party toolkit that can manage Python dependencies, environments, and can be used for packaging and distribution of Python projects.

Poetry completes dependency management, environment configuration, basic information configuration and other functions through the configuration file pyproject.toml. It is equivalent to the integration of Pipfile, setup.py, setup.cfg, requirements.txt and MANIFEST.in in the Python project.

Through the pyproject.toml file, you can not only configure dependency packages, but also can be used to distinguish between development, testing, production environment, and configure source paths.

Why choose poetry?

Poetry has many obvious advantages over pipenv:

More powerful dependency handling

Easy to package and build Python projects

Easy to release toolkit

Structured presentation of dependencies

More powerful dependency handling

Use the previous example of pipenv to show the power of poetry in dependency processing.

Pipenv will directly report an error and terminate when it encounters an intuitive problem with the installation kit.

Using poetry installation will not

$poetry add oslo.utils=1.4.0-Installing pytz (2018.3)-Installing netifaces (0.10.6)-Installing netaddr (0.7.19)-Installing oslo.i18n (2.1.0)-Installing iso8601 (0.1.12)-Installing six (1.11.0)-Installing babel (2.5.3)-Installing pbr (0.11.1)-Installing oslo.utils (1.4.0)

When poetry encounters pbr (> = 0.6, ├──, 0.7, chardet, April 17, 2017)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 262

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report