Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if python package is latest version programmatically?

Tags:

python

pip

gekko

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?

like image 768
Joseph Avatar asked Oct 31 '19 17:10

Joseph


People also ask

How do I check pip package version?

Method 1: pip show To check which version of a given package is installed, use the pip show <your_package> command. For example, to check the version of your NumPy installation or virtual environment, run pip show numpy in your command line or Powershell (Windows), or terminal (macOS and Linux/Ubuntu).

How do I find my PYPI version?

Command line interface To fetch all available package commands, use the following command: $ pypi-version --help Usage: pypi-version [OPTIONS] COMMAND [ARGS]... Command line interface for PyPi version checking. Options: --version Show the version and exit.

How do I update a Python package to a specific version?

How do I Install a Specific Version of a Python Package? To install a specific version of a Python package you can use pip: pip install YourPackage==YourVersion .


1 Answers

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

import subprocess import sys def check(name):     latest_version = str(subprocess.run([sys.executable, '-m', 'pip', 'install', '{}==random'.format(name)], capture_output=True, text=True))     latest_version = latest_version[latest_version.find('(from versions:')+15:]     latest_version = latest_version[:latest_version.find(')')]     latest_version = latest_version.replace(' ','').split(',')[-1]      current_version = str(subprocess.run([sys.executable, '-m', 'pip', 'show', '{}'.format(name)], capture_output=True, text=True))     current_version = current_version[current_version.find('Version:')+8:]     current_version = current_version[:current_version.find('\\n')].replace(' ','')       if latest_version == current_version:         return True     else:         return False 

Edit 2021: The code below no longer works with the new version of pip

The following code calls for pip list --outdated:

import subprocess import sys  def check(name):     reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'list','--outdated'])     outdated_packages = [r.decode().split('==')[0] for r in reqs.split()]     return name in outdated_packages 
like image 68
Yusuf Baktir Avatar answered Oct 24 '22 12:10

Yusuf Baktir