def _get_installed_version(package, module=None): """ Obtains the installed package version using `importlib_metadata.version`. If it fails, use `__import__(module or package).__version__`. """ try: version = importlib_metadata.version(package) except importlib_metadata.PackageNotFoundError: # Note `importlib_metadata.version(package)` is not necessarily equal to # `__import__(package).__version__`. See the example for pytorch below. # # Example # ------- # $ pip install torch==1.9.0 # $ python -c "import torch; print(torch.__version__)" # 1.9.0+cu102 # $ python -c "import importlib_metadata; print(importlib_metadata.version('torch'))" # 1.9.0 version = __import__(module or package).__version__ # Strip the suffix from `dev` versions of PySpark, which are not available for installation # from Anaconda or PyPI if package == "pyspark": version = _strip_dev_version_suffix(version) return version
def _get_installed_version(package, module=None): """ Obtains the installed package version using `importlib_metadata.version`. If it fails, use `__import__(module or package).__version__`. """ try: version = importlib_metadata.version(package) except importlib_metadata.PackageNotFoundError: # Note `importlib_metadata.version(package)` is not necessarily equal to # `__import__(package).__version__`. See the example for pytorch below. # # Example # ------- # $ pip install torch==1.9.0 # $ python -c "import torch; print(torch.__version__)" # 1.9.0+cu102 # $ python -c "import importlib_metadata; print(importlib_metadata.version('torch'))" # 1.9.0 version = __import__(module or package).__version__ # In Databricks, strip a dev version suffix for pyspark (e.g. '3.1.2.dev0' -> '3.1.2') # and make it installable from PyPI. if package == "pyspark" and is_in_databricks_runtime(): version = _strip_dev_version_suffix(version) return version
def test_strip_dev_version_suffix(): assert _strip_dev_version_suffix("1.0.dev0") == "1.0" assert _strip_dev_version_suffix("1.0dev0") == "1.0" assert _strip_dev_version_suffix("1.0.dev") == "1.0" assert _strip_dev_version_suffix("1.0") == "1.0"