Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Improve the docs regarding the migration from imp to importlib #104212

Open
alexprengere opened this issue May 5, 2023 · 3 comments
Open

Improve the docs regarding the migration from imp to importlib #104212

alexprengere opened this issue May 5, 2023 · 3 comments
Labels
3.11 bug and security fixes 3.12 new features, bugs and security fixes docs Documentation in the Doc dir

Comments

@alexprengere
Copy link
Contributor

Now that the imp removal has landed on main, users migrating to Python3.12 will likely need help to move to importlib.
The current imp docs have tips on how to do just that. Great!

One caveat: the imp.load_source has been removed from the docs a long time ago, now it is only visible is the Python2 version of the docs. So users of imp.load_source cannot rely on the docs to help them migrate, and have to Google this. The first results on stackoverflow are a bit wrong:

  • many point to SourceFileLoader(...).load_module(), but this is also deprecated and slated for removal in 3.12 (according to the warning)
  • some solutions point to importlib.util.spec_from_file_location, but this does not work with files that do not end with ".py"

The solution that I think is the best translation:

def imp_load_source(module_name, module_path):
    loader = SourceFileLoader(module_name, module_path)
    module = types.ModuleType(loader.name)
    loader.exec_module(module)
    return module

I think it would be beneficial to have that kind of information in the docs. Unfortunately, imp.load_source is not officially documented, but there are several GitHub issues and SO threads discussing how to migrate code to importlib. IMO, we should do one of:

  • add it back to the docs, with explanations on how to migrate it to importlib
  • just document the migration in the release notes, in the section "Porting to Python 3.12"

What do you think?

@alexprengere alexprengere added the docs Documentation in the Doc dir label May 5, 2023
@warsaw
Copy link
Member

warsaw commented May 5, 2023

Maybe we need a porting guide in the importlib docs for porting older imp and other APIs to importlib? I don't like that imp module has been deleted but the docs are still there.

@hugovk
Copy link
Member

hugovk commented May 5, 2023

Good idea!

In fact we were just discussing this earlier today and thought about putting a migration guide in both the imp and importlib docs and in the 3.12 porting guide.

cc @vstinner

@hugovk hugovk added 3.11 bug and security fixes 3.12 new features, bugs and security fixes labels May 5, 2023
@terryjreedy
Copy link
Member

In current 3.12, 'imp' is no longer present in the module index, which suggests that it is gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 bug and security fixes 3.12 new features, bugs and security fixes docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

4 participants