r"""Check minimum library version required. Parameters ---------- library : str The library name to import. Must have a ``__version__`` property. min_version : str The minimum version string. Anything that matches ``'(\d+ | [a-z]+ | \.)'``. Can also be empty
(library, min_version="0.0", *, strip=True, return_version=False)
| 78 | |
| 79 | |
| 80 | def check_version(library, min_version="0.0", *, strip=True, return_version=False): |
| 81 | r"""Check minimum library version required. |
| 82 | |
| 83 | Parameters |
| 84 | ---------- |
| 85 | library : str |
| 86 | The library name to import. Must have a ``__version__`` property. |
| 87 | min_version : str |
| 88 | The minimum version string. Anything that matches |
| 89 | ``'(\d+ | [a-z]+ | \.)'``. Can also be empty to skip version |
| 90 | check (just check for library presence). |
| 91 | strip : bool |
| 92 | If True (default), then PEP440 development markers like ``.devN`` |
| 93 | will be stripped from the version. This makes it so that |
| 94 | ``check_version('mne', '1.1')`` will be ``True`` even when on version |
| 95 | ``'1.1.dev0'`` (prerelease/dev version). This option is provided for |
| 96 | backward compatibility with the behavior of ``LooseVersion``, and |
| 97 | diverges from how modern parsing in ``packaging.version.parse`` works. |
| 98 | |
| 99 | .. versionadded:: 1.0 |
| 100 | return_version : bool |
| 101 | If True (default False), also return the version (can be None if the |
| 102 | library is missing). |
| 103 | |
| 104 | .. versionadded:: 1.0 |
| 105 | |
| 106 | Returns |
| 107 | ------- |
| 108 | ok : bool |
| 109 | True if the library exists with at least the specified version. |
| 110 | version : str | None |
| 111 | The version. Only returned when ``return_version=True``. |
| 112 | """ |
| 113 | ok = True |
| 114 | version = None |
| 115 | try: |
| 116 | library = import_module(library) |
| 117 | except ImportError: |
| 118 | ok = False |
| 119 | else: |
| 120 | check_version = min_version and min_version != "0.0" |
| 121 | get_version = check_version or return_version |
| 122 | if get_version: |
| 123 | version = library.__version__ |
| 124 | if strip: |
| 125 | version = _strip_dev(version) |
| 126 | if check_version: |
| 127 | if _compare_version(version, "<", min_version): |
| 128 | ok = False |
| 129 | out = (ok, version) if return_version else ok |
| 130 | return out |
| 131 | |
| 132 | |
| 133 | def _strip_dev(version): |