avatarEryk Lewinson

Summary

The pur library simplifies the process of updating a Python requirements.txt file by automating the version updates for listed dependencies.

Abstract

The pur library, which stands for pip update requirements, is a Python tool designed to streamline the management of project dependencies by updating the requirements.txt file with the latest versions of libraries. It emphasizes the importance of keeping libraries up-to-date to address bugs, security vulnerabilities, and compatibility issues. The library does not install or update the actual libraries in the environment; it only modifies the requirements file, leaving the user to install the updated versions manually. This approach can be particularly beneficial for projects with numerous dependencies, and it offers options to review updates for each library interactively to prevent potential conflicts with fixed version requirements.

Opinions

  • The author believes that maintaining current software versions is crucial for bug fixes, security patches, and compatibility.
  • pur is praised for its simplicity and effectiveness in updating requirements.txt files, especially for larger projects.
  • The author stresses the importance of using pur with caution, particularly when certain dependencies must remain at specific versions to ensure code stability.
  • The library is described as user-friendly, with basic usage that is straightforward and an interactive mode for more controlled updates.
  • The author encourages constructive feedback and invites readers to engage on Twitter or through article comments.
Image generated with Midjourney

pur — the easiest way to keep your requirements file up to date

Update all the libraries in your requirements.txt with a single line of code

I don’t think I need to convince you about the benefits of keeping your Python libraries (or other software as a matter of fact) up to date: bugs are fixed over time, potential security vulnerabilities are patched, compatibility issues may arise, etc. And the list goes on and on.

In a Python project, we often use a requirements.txt file as a source of information on which libraries (and which of their versions) should be used in our Python environment. When we want to update the libraries, we most frequently update them in our environment and then modify the requirements file accordingly.

However, there is a simpler way. I have recently discovered a small Python library that can be really helpful in maintaining a clean requirements.txt file and speeding up the update process, especially for projects with a larger list of dependencies.

Photo by Tran Mau Tri Tam on Unsplash

pur in action

pur stands for pip update requirements and is a small Python library that can update a requirements file with a single command. Its basic usage is very simple and we will illustrate it with an example.

First, we need to install pur:

pip install pur

Let’s imagine that we are in a virtual environment and we have the project’s requirements.txt file that contains the following:

pandas==1.2.4
yfinance==0.1.63

It’s a very simple example, but it suits our purpose. Also, we are aware that both libraries are outdated. In such a case, we can use the pur library by running the following command in the terminal:

pur -r requirements.txt

Which modifies the requirements.txt with the latest versions of the libraries listed in the file. When doing so, it prints the following:

Updated pandas: 1.2.4 -> 1.4.1
Updated yfinance: 0.1.63 -> 0.1.70
All requirements up-to-date

At this point, it is important to emphasize one crucial thing: the library never modifies the environment itself, that is, it will neither install nor update any libraries. It only does one thing — updates the requirements file. Having said that, now we actually need to install the updates of the libraries, for example, by running the following command:

pip install -r requirements.txt

Which installs the latest versions of the libraries — 0.1.70 for yfinance and 1.4.1 for pandas. At this point, the installation would fail in case there were some conflicts with the versions of the dependencies.

pur also offers additional options, for example, to iteratively ask about updating each of the libraries in the requirements file. This might be useful in cases in which we know that some of the libraries actually need to be fixed to certain versions, otherwise things will break.

Takeaways

  • pur is a handy, lightweight library that takes care of updating the requirements.txt file with the latest versions of the libraries,
  • the library never installs/updates the libraries, it only modifies the requirements file,
  • the library should be used with caution when some of the versions need to remain fixed for our code to work properly.

Also, any constructive feedback is welcome. You can reach out to me on Twitter or in the comments.

Liked the article? Become a Medium member to continue learning by reading without limits. If you use this link to become a member, you will support me at no extra cost to you. Thanks in advance and see you around!

You might also be interested in one of the following:

References

Python
Programming
Data Science
Education
Software Development
Recommended from ReadMedium