def test_get_installed_version(): import numpy as np import pandas as pd assert _get_installed_version("mlflow") == mlflow.__version__ assert _get_installed_version("numpy") == np.__version__ assert _get_installed_version("pandas") == pd.__version__
def test_get_installed_version(tmpdir): import numpy as np import pandas as pd import sklearn assert _get_installed_version("mlflow") == mlflow.__version__ assert _get_installed_version("numpy") == np.__version__ assert _get_installed_version("pandas") == pd.__version__ assert _get_installed_version("scikit-learn", module="sklearn") == sklearn.__version__ not_found_package = tmpdir.join("not_found.py") not_found_package.write("__version__ = '1.2.3'") sys.path.insert(0, tmpdir.strpath) with pytest.raises(importlib_metadata.PackageNotFoundError): importlib_metadata.version("not_found") assert _get_installed_version("not_found") == "1.2.3"
def _is_available_on_pypi(package, version=None, module=None): """ Returns True if the specified package version is available on PyPI. :param package: The name of the package. :param version: The version of the package. If None, defaults to the installed version. :param module: The name of the top-level module provided by the package . For example, if `package` is 'scikit-learn', `module` should be 'sklearn'. If None, defaults to `package`. """ resp = requests.get("https://pypi.python.org/pypi/{}/json".format(package)) if not resp.ok: return False version = version or _get_installed_version(module or package) dist_files = resp.json()["releases"].get(version) return ( dist_files is not None # specified version exists and (len(dist_files) > 0) # at least one distribution file exists and not dist_files[0].get("yanked", False) # specified version is not yanked )