_optional.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import distutils.version
  2. import importlib
  3. import types
  4. import warnings
  5. # Update install.rst when updating versions!
  6. VERSIONS = {
  7. "bs4": "4.6.0",
  8. "bottleneck": "1.2.1",
  9. "fastparquet": "0.3.2",
  10. "gcsfs": "0.2.2",
  11. "lxml.etree": "3.8.0",
  12. "matplotlib": "2.2.2",
  13. "numexpr": "2.6.2",
  14. "odfpy": "1.3.0",
  15. "openpyxl": "2.5.7",
  16. "pandas_gbq": "0.8.0",
  17. "pyarrow": "0.13.0",
  18. "pytables": "3.4.2",
  19. "pytest": "5.0.1",
  20. "pyxlsb": "1.0.6",
  21. "s3fs": "0.3.0",
  22. "scipy": "0.19.0",
  23. "sqlalchemy": "1.1.4",
  24. "tables": "3.4.2",
  25. "tabulate": "0.8.3",
  26. "xarray": "0.8.2",
  27. "xlrd": "1.1.0",
  28. "xlwt": "1.2.0",
  29. "xlsxwriter": "0.9.8",
  30. "numba": "0.46.0",
  31. }
  32. def _get_version(module: types.ModuleType) -> str:
  33. version = getattr(module, "__version__", None)
  34. if version is None:
  35. # xlrd uses a capitalized attribute name
  36. version = getattr(module, "__VERSION__", None)
  37. if version is None:
  38. raise ImportError(f"Can't determine version for {module.__name__}")
  39. return version
  40. def import_optional_dependency(
  41. name: str, extra: str = "", raise_on_missing: bool = True, on_version: str = "raise"
  42. ):
  43. """
  44. Import an optional dependency.
  45. By default, if a dependency is missing an ImportError with a nice
  46. message will be raised. If a dependency is present, but too old,
  47. we raise.
  48. Parameters
  49. ----------
  50. name : str
  51. The module name. This should be top-level only, so that the
  52. version may be checked.
  53. extra : str
  54. Additional text to include in the ImportError message.
  55. raise_on_missing : bool, default True
  56. Whether to raise if the optional dependency is not found.
  57. When False and the module is not present, None is returned.
  58. on_version : str {'raise', 'warn'}
  59. What to do when a dependency's version is too old.
  60. * raise : Raise an ImportError
  61. * warn : Warn that the version is too old. Returns None
  62. * ignore: Return the module, even if the version is too old.
  63. It's expected that users validate the version locally when
  64. using ``on_version="ignore"`` (see. ``io/html.py``)
  65. Returns
  66. -------
  67. maybe_module : Optional[ModuleType]
  68. The imported module, when found and the version is correct.
  69. None is returned when the package is not found and `raise_on_missing`
  70. is False, or when the package's version is too old and `on_version`
  71. is ``'warn'``.
  72. """
  73. msg = (
  74. f"Missing optional dependency '{name}'. {extra} "
  75. f"Use pip or conda to install {name}."
  76. )
  77. try:
  78. module = importlib.import_module(name)
  79. except ImportError:
  80. if raise_on_missing:
  81. raise ImportError(msg) from None
  82. else:
  83. return None
  84. minimum_version = VERSIONS.get(name)
  85. if minimum_version:
  86. version = _get_version(module)
  87. if distutils.version.LooseVersion(version) < minimum_version:
  88. assert on_version in {"warn", "raise", "ignore"}
  89. msg = (
  90. f"Pandas requires version '{minimum_version}' or newer of '{name}' "
  91. f"(version '{version}' currently installed)."
  92. )
  93. if on_version == "warn":
  94. warnings.warn(msg, UserWarning)
  95. return None
  96. elif on_version == "raise":
  97. raise ImportError(msg)
  98. return module