tslearn-team/tslearn

0.6.0 fails to import when pytorch is not installed with ModuleNotFoundError: No module named 'torch'

eicca opened this issue · 0 comments

eicca commented

Description

The latest release includes new pytorch backend. Unfortunately, when pytorch is not installed, it fails to fallback to a numpy backend. I managed to reproduce this on two different CI environments (ubuntu linux with python 3.10 and 3.9) and locally (WSL ubuntu with python 3.9).

How to reproduce

Install latest tslearn in a fresh environment:

➜  python --version
Python 3.9.16
➜  python -m venv .venv
➜  source .venv/bin/activate
(.venv) ➜ pip install tslearn
Collecting tslearn
  Using cached tslearn-0.6.0-py3-none-any.whl (368 kB)
Collecting numpy
  Downloading numpy-1.25.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.7/17.7 MB 9.2 MB/s eta 0:00:00
Collecting numba
  Downloading numba-0.57.1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (3.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.6/3.6 MB 8.8 MB/s eta 0:00:00
Collecting joblib
  Using cached joblib-1.3.1-py3-none-any.whl (301 kB)
Collecting scikit-learn
  Using cached scikit_learn-1.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.9 MB)
Collecting scipy
  Using cached scipy-1.11.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (36.5 MB)
Collecting numpy
  Downloading numpy-1.24.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.3/17.3 MB 5.5 MB/s eta 0:00:00
Collecting llvmlite<0.41,>=0.40.0dev0
  Downloading llvmlite-0.40.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (42.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.1/42.1 MB 7.5 MB/s eta 0:00:00
Collecting threadpoolctl>=2.0.0
  Using cached threadpoolctl-3.1.0-py3-none-any.whl (14 kB)
Installing collected packages: threadpoolctl, numpy, llvmlite, joblib, scipy, numba, scikit-learn, tslearn
Successfully installed joblib-1.3.1 llvmlite-0.40.1 numba-0.57.1 numpy-1.24.4 scikit-learn-1.3.0 scipy-1.11.1 threadpoolctl-3.1.0 tslearn-0.6.0
WARNING: You are using pip version 22.0.4; however, version 23.1.2 is available.
You should consider upgrading via the '/home/me/tmp/.venv/bin/python -m pip install --upgrade pip' command.
(.venv) ➜  python -c "import tslearn.metrics"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/me/tmp/.venv/lib/python3.9/site-packages/tslearn/metrics/__init__.py", line 8, in <module>
    from .dtw_variants import (dtw, dtw_limited_warping_length,
  File "/home/me/tmp/.venv/lib/python3.9/site-packages/tslearn/metrics/dtw_variants.py", line 6, in <module>
    from tslearn.backend import instantiate_backend
  File "/home/me/tmp/.venv/lib/python3.9/site-packages/tslearn/backend/__init__.py", line 8, in <module>
    from .pytorch_backend import PyTorchBackend
  File "/home/me/tmp/.venv/lib/python3.9/site-packages/tslearn/backend/pytorch_backend.py", line 18, in <module>
    import torch as _torch
ModuleNotFoundError: No module named 'torch'

Expected behavior

I would assume that the importing will fall back to numpy backend when pytorch is not installed via this exception handling https://github.com/tslearn-team/tslearn/blob/main/tslearn/backend/backend.py#L9

I think something is not going as expected while catching the import error exception, but I can't pin point right now what exactly.

Environments

Local:

  • OS: Ubuntu 20.04
  • python 3.9.16
  • tslearn 0.6.0

CI:

  • OS: Ubuntu 22.04.2
  • python 3.10.11
  • tslearn 0.6.0