How can I safely check if a python package is out of date?

I want to include some kind of self-updating mechanism in the python package, and I do not want to cause the code to be updated before the script runs, since it is very slow, I'm looking for a smart mechanism.

Each time it is used, it can make an HTTP call, possibly for PyPi, and if it detects a new version, it will display a warning on STDERR.

Obviously, as part of this process, I would also like to cache the result of the last call, so the library will not check for updates more than once a day, say.

Is there something like this already implemented? Do you have an example that can be used to cache results from HTTP calls between different executions so that it does not impose significant delays?

+4
source share
2 answers

I wrote a library outdatedto solve this problem. The fastest way to use it is to insert such code somewhere in the root of your package:

from outdated import warn_if_outdated

warn_if_outdated('my-package-name', '1.2.3')

This will result in a warning about stderr on request, caching an HTTP call for 24 hours or more. Details can be found at the link above.

It is also much faster than pip list -owithout even caching.

+1
source

To show outdated packages, you can simply run pip list -o, but this is not related to any caching.

Although it would be trivial to simply add pip list -o > outdated.txtto cronjob so that it automatically updates daily :)

Here is a sample code to use pipas a library:

def get_outdated():
    import pip

    list_command = pip.commands.list.ListCommand()
    options, args = list_command.parse_args([])
    packages = pip.utils.get_installed_distributions()

    return list_command.get_outdated(packages, options)

print(get_outdated())
+7
source

All Articles