How do you check if a package is at its latest version programmatically in a script and return a true or false?
I can check with a script like this:
package='gekko'
import pip
if hasattr(pip, 'main'):
from pip import main as pipmain
else:
from pip._internal import main as pipmain
pipmain(['search','gekko'])
or with command line:
(base) C:\User>pip search gekko
gekko (0.2.3) - Machine learning and optimization for dynamic systems
INSTALLED: 0.2.3 (latest)
But how do I check programmatically and return true or false?
Latest version:
My project
luddite
has this feature:Installed version:
The canonical way to check installed version is just to access the
__version__
attribute of the top-level namespace:Unfortunately not all projects set this attribute. When they don't, you can use
pkg_resources
to dig it out from the metadata:This should do the trick at least for demo purposes. Simply call
isLatestVersion
with the name of the package you would like to check. If you are using this somewhere important you would want to try/catch the url request as internet access may not be available. Also note that if the package is not installedisLatestVersion
will return False.This is tested for Python 3.7.4 and Pip 19.0.3.
My project
johnnydep
has this feature.In shell:
In Python:
It's not hard to write a simple script yourself by querying the PyPI API. With the latest Python 3.8, it's possible using only the standard library (when using Python 3.7 or older, you'll have to install the
importlib_metadata
backport):Usage example:
If you're happen to have
packaging
installed, it's a better alternative todistutils.version
for version parsing:becomes
Edit: Remove pip search
Thanks for the several suggestions. Here is a new version that doesn't use
pip search
but instead pulls the latest version directly frompypi
as proposed by Daniel Hill. This also resolves the issue with the substring false matches.Original Response
Here is a fast solution that retrieves latest version information on only the
gekko
package of interest.This produces the message
Latest version (0.2.3) of gekko is installed
and returnsTrue
to indicate latest version (orFalse
if not the latest version). This may not be the best solution because it only checks for a version substring withif d[name] in s.decode():
but it is faster thanpip list --outdated
that checks all the packages. This isn't the most reliable method because it will return an incorrectTrue
if current installed version is0.2.3
but latest version is0.2.30
or0.2.3a
. An improvement would be to programmatically get the latest version and do a direct comparison.Fast Version (Checking the package only)
The code below calls the package with an unavailable version like
pip install package_name==random
. The call returns all the available versions. The program reads the latest version.The program then runs
pip show package_name
and gets the current version of the package.If it finds a match, it returns True, otherwise False.
This is a reliable option given that it stands on
pip
The following code calls for
pip list --outdated
: