Skip to content

bug: import transformer before skops causes ModuleNotFoundError #527

Description

@H4M5TER

Import transformer before skops.io will cause ModuleNotFoundError without torchvision installed

Minimal reproduction

Python 3.12.6 (main, Sep  9 2024, 20:50:27) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import transformers   
>>> import skops.io as sio
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\dev\bertalign\.venv\Lib\site-packages\skops\io\__init__.py", line 1, in <module>
    from ._persist import dump, dumps, get_untrusted_types, load, loads
  File "D:\dev\bertalign\.venv\Lib\site-packages\skops\io\_persist.py", line 22, in <module>
    module = importlib.import_module(module_name, package="skops.io")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\zhuxx\AppData\Roaming\uv\python\cpython-3.12.6-windows-x86_64-none\Lib\importlib\__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\dev\bertalign\.venv\Lib\site-packages\skops\io\_general.py", line 17, in <module>
    from ._trusted_types import (
  File "D:\dev\bertalign\.venv\Lib\site-packages\skops\io\_trusted_types.py", line 91, in <module>
    SCIPY_UFUNC_TYPE_NAMES = get_public_type_names(
                             ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\dev\bertalign\.venv\Lib\site-packages\skops\io\_utils.py", line 238, in get_public_type_names
    and (type_name := get_type_name(obj)).startswith(module_name)
                      ^^^^^^^^^^^^^^^^^^
  File "D:\dev\bertalign\.venv\Lib\site-packages\skops\io\_utils.py", line 183, in get_type_name
    return f"{get_module(t)}.{t.__name__}"
              ^^^^^^^^^^^^^
  File "D:\dev\bertalign\.venv\Lib\site-packages\skops\io\_utils.py", line 90, in get_module
    return whichmodule(obj, obj.__name__)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\dev\bertalign\.venv\Lib\site-packages\skops\io\_utils.py", line 53, in whichmodule
    if _getattribute(module, name)[0] is obj:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\dev\bertalign\.venv\Lib\site-packages\skops\io\_utils.py", line 25, in _getattribute
    obj = getattr(obj, subpath)
          ^^^^^^^^^^^^^^^^^^^^^
  File "D:\dev\bertalign\.venv\Lib\site-packages\transformers\__init__.py", line 862, in _getattr
    return getattr(importlib.import_module(target, __name__), name)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\zhuxx\AppData\Roaming\uv\python\cpython-3.12.6-windows-x86_64-none\Lib\importlib\__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\dev\bertalign\.venv\Lib\site-packages\transformers\models\aria\image_processing_aria.py", line 23, in <module>
    from torchvision.transforms.v2 import functional as tvF
ModuleNotFoundError: No module named 'torchvision'

My case

For me, skops is a transitive dependency used by wtpsplit, which had worked around this problem by itself. But my code loaded transformers before wtpsplit, so there is still problem. As a library, wtpsplit is not possible to control its load order.

https://github.com/segment-any-text/wtpsplit/blob/e48922c0ad240c7d2cb1ef8afbc0cd413372bc83/wtpsplit/__init__.py#L12-L15

# Import skops before transformers: skops enumerates trusted types at import time
# and can otherwise trigger transformers' lazy vision submodules (torchvision), which
# wtpsplit does not use for segmentation.
import skops.io as sio

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions