Replace deprecated imp module with importlib
🌱 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:
- Raised
PendingDeprecationWarningsince 3.4 (2014) - Raised
DeprecationWarningsince 3.5 (2015) - Updated
DeprecationWarningto say removal in 3.12 since 3.10 (2021) - Removal planned for 3.12 (2023)
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.
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
impdocs 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?
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 - yes, this is very helpful. Thank you.
@hugovk - yes, this is very helpful. Thank you. I addressed and fixed this issue in https://github.com/apple/coremltools/pull/2170
Fixed by PR 2170