coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

Replace deprecated imp module with importlib

Open hugovk opened this issue 2 years ago • 3 comments

🌱 Describe your Feature Request

This library uses the imp module which has been deprecated since Python 3.4 and set for removal in 3.12:

Python 3.12 is set for release on 2023-10-02 and this library is one of the top 5,000 most-downloaded from PyPI.

Please could you upgrade to use importlib? The imp docs have suggestions on what to use to replace each function and constant.

hugovk avatar Apr 28 '23 20:04 hugovk

I agree. This is something we'll need to address before we can support Python 3.12.

Our only usage of imp is in setup.py. We are only using load_source from imp.

The imp docs have suggestions on what to use to replace each function and constant.

I'm actually not seeing any suggestions for load_source.

@hugovk - any ideas what we should be using?

TobyRoseman avatar May 01 '23 20:05 TobyRoseman

See https://github.com/pyinvoke/invoke/pull/215 for a PR that replaced it with importlib.machinery import SourceFileLoader a few years back.

That code was just updated now looks like this:

from importlib.util import spec_from_loader
from types import ModuleType

try:
    from importlib.machinery import SourceFileLoader
except ImportError:  # PyPy3
    from importlib._bootstrap import (  # type: ignore[no-redef]
        _SourceFileLoader as SourceFileLoader,
    )


def load_source(name: str, path: str) -> Dict[str, Any]:
    if not os.path.exists(path):
        return {}
    loader = SourceFileLoader("mod", path)
    mod = ModuleType("mod")
    mod.__spec__ = spec_from_loader("mod", loader)
    loader.exec_module(mod)
    return vars(mod)

Does that help?

hugovk avatar May 02 '23 06:05 hugovk

@hugovk - yes, this is very helpful. Thank you.

TobyRoseman avatar May 02 '23 17:05 TobyRoseman

@hugovk - yes, this is very helpful. Thank you. I addressed and fixed this issue in https://github.com/apple/coremltools/pull/2170

teelrabbit avatar Mar 16 '24 20:03 teelrabbit

Fixed by PR 2170

YifanShenSZ avatar Mar 19 '24 23:03 YifanShenSZ