
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.

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 purLet’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.63It’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.txtWhich 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-dateAt 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.txtWhich 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
puris a handy, lightweight library that takes care of updating therequirements.txtfile 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:






