diff --git a/torchaudio/common_utils.py b/torchaudio/common_utils.py index 28b77200a6..014f4392ce 100644 --- a/torchaudio/common_utils.py +++ b/torchaudio/common_utils.py @@ -1,7 +1,4 @@ -import sys - -PY3 = sys.version_info > (3, 0) -PY34 = sys.version_info >= (3, 4) +import importlib.util def _check_module_exists(name: str) -> bool: @@ -11,28 +8,10 @@ def _check_module_exists(name: str) -> bool: our tests, e.g., setting multiprocessing start method when imported (see librosa/#747, torchvision/#544). """ - if not PY3: # Python 2 - import imp - try: - imp.find_module(name) - return True - except ImportError: - return False - elif not PY34: # Python [3, 3.4) - import importlib - loader = importlib.find_loader(name) - return loader is not None - else: # Python >= 3.4 - import importlib - import importlib.util - spec = importlib.util.find_spec(name) - return spec is not None + spec = importlib.util.find_spec(name) + return spec is not None IMPORT_NUMPY = _check_module_exists('numpy') IMPORT_KALDI_IO = _check_module_exists('kaldi_io') IMPORT_SCIPY = _check_module_exists('scipy') - -# On Py2, importing librosa 0.6.1 triggers a TypeError (if using newest joblib) -# see librosa/librosa#729. -# TODO: allow Py2 when librosa 0.6.2 releases -IMPORT_LIBROSA = _check_module_exists('librosa') and PY3 +IMPORT_LIBROSA = _check_module_exists('librosa')