Skip to content

Instantly share code, notes, and snippets.

@stas00
Last active December 14, 2018 02:44
Show Gist options
  • Save stas00/074a947d77a0cc1e956f21b60c1e3aa9 to your computer and use it in GitHub Desktop.
Save stas00/074a947d77a0cc1e956f21b60c1e3aa9 to your computer and use it in GitHub Desktop.
a little function that checks whether a given module version is availble on pypi servers
# usage:
# pypi_module_version_is_available("Pillow", "5.4.0")
import subprocess
def pypi_module_version_is_available(module, version):
"Check whether module==version is available on pypi"
# returns True/False (or None if failed to execute the check)
# using a hack that when passing "module==" w/ no version number to pip
# it "fails" and returns all the available versions in stderr
try:
cmd = f"pip install {module}=="
result = subprocess.run(cmd.split(), shell=False, check=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except Exception as e:
print(f"Error: {e}")
return None
else:
if result.returncode == 1 and result.stderr:
output = result.stderr.decode('utf-8')
return True if version in output else False
else:
print(f"Some error in {cmd}")
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment