GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||
GeezerVison is showing github.com — gh-104212: Explain how to port imp code to importlib by vstinner · Pull Request #105905 · python/cpython Conversation
|
|
Replacing load_dynamic() is non-trivial: def load_dynamic(name, filename): loader = importlib.machinery.ExtensionFileLoader(name, filename) spec = importlib.util.spec_from_loader(name, loader) module = importlib.util.module_from_spec(spec) sys.modules[module.__name__] = module loader.exec_module(module) return module If you omit |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Replace init_builtin(): def init_builtin(name): spec = importlib.machinery.BuiltinImporter.find_spec(name) if spec is None: raise ImportError(f'no built-in module named {name!r}') return importlib.util.module_from_spec(spec) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
For removed imp.load_source(), I proposed adding importlib.util.load_source_path(): PR #105755. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Replace import sys import os.path import importlib def load_package(name, path): old_path = list(sys.path) try: sys.path.insert(0, os.path.dirname(path)) return importlib.import_module(name) finally: sys.path.clear() sys.path.extend(old_path) I'm not sure about this one. It uses high-level API to call indirectly PathFinder and FileFinder API. I would prefer to not expose them since I consider them as the internal API. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I suppose that it can be used to replace Otherwise, here is a recipe to replace import importlib.util import sys def load_compiled(name, filename): spec = importlib.util.spec_from_file_location(name, filename) module = importlib.util.module_from_spec(spec) sys.modules[module.__name__] = module spec.loader.exec_module(module) return module Test: import os.path def create_pyc(): name = 'script' filename = f'/tmp/{name}.py' with open(filename, "w") as fp: print("print('hello')", file=fp) sys.path.insert(0, os.path.dirname(filename)) mod = importlib.import_module(name) del sys.path[0] del sys.modules[name] os.unlink(filename) mod = None return importlib.util.cache_from_source(filename) print("create PYC") pyc = create_pyc() print(f"load PYC: {pyc}") mod = load_compiled('script', pyc) print(mod) print(mod.__file__) print(mod.__cached__) os.unlink(pyc) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
My recipes always create a new module: they don't try to get a cached module from |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Explain in What's New in Python 3.12 how to port existing code using the removed imp to the importlib module.
|
Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
GH-105952 is a backport of this pull request to the 3.12 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…105905) (cherry picked from commit 7a56a41) Co-authored-by: Victor Stinner <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.