This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: importlib: Document how to replace load_module() in What's New in Python 3.10
Type: Stage: resolved
Components: Documentation, Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: docs@python Nosy List: The Compiler, brett.cannon, docs@python, hroncok, vstinner, webknjaz
Priority: normal Keywords:

Created on 2021-03-18 10:19 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (7)
msg389007 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-03-18 10:19
The load_module() method of importlib loaders is deprecated which cause test failures in multiple projects.

It is not easy to guess how to replace it. Examples:

* pkg_resources fix adding create_module() and exec_module() methods: https://github.com/pypa/setuptools/commit/6ad2fb0b78d11e22672f56ef9d65d13ebd3475a9
* pkg_resources fix replacing importlib.load_module() function call (not loader methods) with importlib.import_module(): https://github.com/pypa/setuptools/commit/a54d9e6b30c6da0542698144d2ff149ae7cadc9a

Cython uses this code:

if sys.version_info[:2] < (3, 3):
    import imp
    def load_dynamic(name, module_path):
        return imp.load_dynamic(name, module_path)
else:
    from importlib.machinery import ExtensionFileLoader
    def load_dynamic(name, module_path):
        return ExtensionFileLoader(name, module_path).load_module()

Fixed Cython code:

if sys.version_info < (3, 5):
    import imp
    def load_dynamic(name, module_path):
        return imp.load_dynamic(name, module_path)
else:
    import importlib.util as _importlib_util
    def load_dynamic(name, module_path):
        spec = _importlib_util.spec_from_file_location(name, module_path)
        module = _importlib_util.module_from_spec(spec)
        # sys.modules[name] = module
        spec.loader.exec_module(module)
        return module
msg389033 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2021-03-18 18:32
The documentation states in the deprecation notice for https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.load_module that create_module() and exec_module() are what are necessary. But the it isn't a direct 1:1 replacement and people are expected to use all of the helper code so it's going to be very specific to the need of the code.
msg390514 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-08 08:56
find_module() and find_loader() are now also deprecated (bpo-42134). Currently, What's New in Python 3.10 says: "(superseded by exec_module())".

It would be great to have a documentation like the good subprocess "replacement" documentation:
https://docs.python.org/dev/library/subprocess.html#subprocess-replacements
msg390515 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-08 09:05
Concrete issue: setuptools defines a VendorImporter class with find_module() method, but no find_spec() method. How can it be ported to Python 3.10?

https://github.com/pypa/setuptools/issues/2632
msg390541 - (view) Author: Sviatoslav Sydorenko (webknjaz) * Date: 2021-04-08 18:41
@vstinner: I think I figure out the solution — https://github.com/pypa/setuptools/pull/2633
msg390556 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2021-04-08 21:44
Much like the question about load_module(), replacing find_module() with find_spec() can be helped with things like importlib.util.spec_from_file_location() and .spec_from_loader() (https://docs.python.org/3/library/importlib.html#importlib.util.spec_from_file_location and https://docs.python.org/3/library/importlib.html#importlib.util.spec_from_loader), but there isn't a 1:1 swap, so it will take some thought.

And the docs in importlib for things like find_module() say to implement find_spec() which then mentions spec_from_loader() may be useful.

As for replicating the subprocess docs approach, that's a little different since those alternatives in the stdlib are not going anywhere while the stuff in importlib is going to be gone in 3.12 so it will only last for two years.

And BTW I took care of six for you knowing you were going to ask about it if I didn't. 😉
msg412231 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-02-01 01:08
Python 3.10 is released. It's too late, I close the issue.
History
Date User Action Args
2022-04-11 14:59:42adminsetgithub: 87706
2022-02-01 01:08:45vstinnersetstatus: open -> closed
resolution: out of date
messages: + msg412231

stage: resolved
2021-04-08 21:44:14brett.cannonsetmessages: + msg390556
2021-04-08 18:41:25webknjazsetnosy: + webknjaz
messages: + msg390541
2021-04-08 11:43:30The Compilersetnosy: + The Compiler
2021-04-08 09:05:50vstinnersetnosy: + hroncok
messages: + msg390515
2021-04-08 08:56:50vstinnersetmessages: + msg390514
2021-03-18 18:32:12brett.cannonsetmessages: + msg389033
2021-03-18 10:19:33vstinnercreate