Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix comparing versions #6434

Merged
merged 20 commits into from
Mar 23, 2021
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed torch distributed not available in setup hook for DDP ([#6506](https://github.com/PyTorchLightning/pytorch-lightning/pull/6506))


- Fixed comparing required versions ([#6434](https://github.com/PyTorchLightning/pytorch-lightning/pull/6434))


## [1.2.4] - 2021-03-16

### Changed
Expand Down
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def package_list_from_file(file):
}
MOCK_PACKAGES = []
if SPHINX_MOCK_REQUIREMENTS:
MOCK_PACKAGES += ['fairscale']
# mock also base packages when we are on RTD since we don't install them there
MOCK_PACKAGES += package_list_from_file(os.path.join(PATH_ROOT, 'requirements.txt'))
MOCK_PACKAGES += package_list_from_file(os.path.join(PATH_ROOT, 'requirements', 'extra.txt'))
Expand Down
18 changes: 15 additions & 3 deletions pytorch_lightning/utilities/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""General utilities"""
import importlib
import operator
import platform
import sys
from distutils.version import LooseVersion
from importlib.util import find_spec

import torch
from pkg_resources import DistributionNotFound, get_distribution
from pkg_resources import DistributionNotFound


def _module_available(module_path: str) -> bool:
Expand All @@ -42,11 +43,22 @@ def _module_available(module_path: str) -> bool:


def _compare_version(package: str, op, version) -> bool:
"""
Compare package version with some requirements

>>> _compare_version("torch", operator.ge, "0.1")
True
"""
if not _module_available(package):
return False
try:
pkg_version = LooseVersion(get_distribution(package).version)
return op(pkg_version, LooseVersion(version))
pkg = importlib.import_module(package)
except DistributionNotFound:
return False
if not hasattr(pkg, '__version__'):
# in case version is not defined always return True as likely it is mocked call
return True
return op(LooseVersion(pkg.__version__), LooseVersion(version))


_IS_WINDOWS = platform.system() == "Windows"
Expand Down
1 change: 1 addition & 0 deletions requirements/extra.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ torchtext>=0.5
# onnx>=1.7.0
onnxruntime>=1.3.0
hydra-core>=1.0
# todo: when user standard package stream, drop `fairscale` from hard mocked docs libs
Borda marked this conversation as resolved.
Show resolved Hide resolved
https://github.com/PyTorchLightning/fairscale/archive/pl_1.2.0.zip