Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace deprecated imp module with importlib #1846

Closed
hugovk opened this issue Apr 28, 2023 · 5 comments
Closed

Replace deprecated imp module with importlib #1846

hugovk opened this issue Apr 28, 2023 · 5 comments
Labels
feature request Functionality does not currently exist, would need to be created as a new feature (type) triaged Reviewed and examined, release as been assigned if applicable (status)

Comments

@hugovk
Copy link

hugovk commented Apr 28, 2023

🌱 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 hugovk added the feature request Functionality does not currently exist, would need to be created as a new feature (type) label Apr 28, 2023
@TobyRoseman
Copy link
Collaborator

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?

@hugovk
Copy link
Author

hugovk commented May 2, 2023

See pyinvoke/invoke#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?

@TobyRoseman
Copy link
Collaborator

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

@teelrabbit
Copy link
Contributor

@hugovk - yes, this is very helpful. Thank you. I addressed and fixed this issue in #2170

@YifanShenSZ
Copy link
Collaborator

Fixed by PR 2170

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
feature request Functionality does not currently exist, would need to be created as a new feature (type) triaged Reviewed and examined, release as been assigned if applicable (status)
Projects
None yet
Development

No branches or pull requests

4 participants