-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Comments
Maybe we need a porting guide in the importlib docs for porting older imp and other APIs to importlib? I don't like that |
Good idea! In fact we were just discussing this earlier today and thought about putting a migration guide in both the cc @vstinner |
In current 3.12, 'imp' is no longer present in the module index, which suggests that it is gone. |
I don't know the importlib design, so maybe it doesn't make sense, but... Would it make sense to add a imp_load_source() function to importlib? Maybe under a different name. |
My own use case of load_module(): old code written first for Python 2 using imp then ported to SourceLoader. It's a short script which looks for "test_xxx.py" files and then run them. Stupid but simple pytest-like, good enough for my needs: https://github.com/vstinner/hachoir/blob/0a030e3c8045441a0c30a04475a94f3aadd818ed/runtests.py#L72 I was already annoyed to have to port to them a first date to get rid of imp. So the code should be updated again? |
Just to note, see, e.g., #58756 for some previous discussion of this. |
Don't use removed importlib load_modules() method. See: python/cpython#104212
The recipe didn't work for me:
I used this recipe instead: def load_module(module_name, filename):
loader = importlib.machinery.SourceFileLoader(module_name, filename)
module = types.ModuleType(loader.name)
module.__file__ = filename
sys.modules[module.__name__] = module
loader.exec_module(module)
return module |
Minor step forward: I documented how to replace removed |
That's actually only true if you don't pass in the loader you need to use as the code has to guess as what you're after. You can use either |
Explain in What's New in Python 3.12 how to port existing code using the removed imp to the importlib module.
Explain in What's New in Python 3.12 how to port existing code using the removed imp to the importlib module.
Explain in What's New in Python 3.12 how to port existing code using the removed imp to the importlib module.
Each time that I have to dig into importlib internals, I'm lost, so I took some notes at: https://pythondev.readthedocs.io/import.html |
Explain in What's New in Python 3.12 how to port existing code using the removed imp to the importlib module.
I'm not sure that this fix was correct in the gsutil-mirrors/boto project: replace imp.find_module() with |
I enhanced the documentation on how to port existing code from imp to importlib: commit 7a56a41 |
Correct, you need to find the spec and then once you have it you can use it to load the module. If someone called |
Explain how to port removed imp "load" functions to Python 3.13 in What's New in Python 3.12.
It could be, but I think it should get removed once 3.11 reaches EOL.
That should probably be removed at this point as no one knows what those modules even do anymore. |
Explain how to port removed imp.load_source() to importlib in What's New in Python 3.12.
I proposed PR #105951 to add recipes to replace the following removed imp functions: load_source(), load_compiled(), load_dynamic(), load_package(), init_builtin(). These functions are special because you can specify a path from where the module is loaded. These functions are like "implementation details" of load_module() which should be used with find_module(). To compare it to importlib design, find_module() produces something like an importlib "spec" (ModuleSpec), and load_module() implements different importlib "loaders" depending on the module type. The problem is that there is no 1-to-1 mapping between imp and importlib since these two modules have very different design on purpose. I'm not comfortable with adding such long wall of text just to update code using imp in What's New in Python 3.12. I'm not convinced that init_builtin() or load_dynamic() are commonly used. These functions are more like internal functions used by the I'm not sure neither how find_module() was used with load_module() to load modules. Is the use case to specify a search path which is not in I still consider that imp.load_source() is different and remains a relevant use case on its own. So I proposed PR #105978 to just document a recipe to replace this one. |
Explain how to port removed imp.load_source() to importlib in What's New in Python 3.12.
Explain how to port removed imp.load_source() to importlib in What's New in Python 3.12.
I think so.
Worried they would forget to clean up |
Explain how to port removed imp.load_source() to importlib in What's New in Python 3.12. (cherry picked from commit 18a7c86) Co-authored-by: Victor Stinner <vstinner@python.org>
That's now done in What's New in Python 3.12, but under the "Removed" section, where removals are mentioned. I dislike documenting the same removal at two places. https://docs.python.org/dev/whatsnew/3.12.html#removed I just wrote a 3.12 backport for my second doc change: PR #106083. It'ss going to be merged soon. I added a recipe to replace imp.load_source(). I consider that this documentation issue is now solved and I close the issue. If someone wants a more complete explanation for a specific removed imp function, please open a new issue. Thanks @alexprengere for the bug report, thanks @brettcannon and @arhadthedev for the reviews. |
Largely based on https://discuss.python.org/t/how-do-i-migrate-from-imp/27885 and other discussions linked from python/cpython#104212.
Python 3.12 deprecated and removed the already deprecated and not documented imp library (the load_source function was never documented) Replace this with a modern alternative suggested by python/cpython#104212. Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Python 3.12 deprecated and removed the already deprecated and not documented imp library (the load_source function was never documented) Replace this with a modern alternative suggested by python/cpython#104212. Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Python 3.12 deprecated and removed the already deprecated and not documented imp library (the load_source function was never documented) Replace this with a modern alternative suggested by python/cpython#104212. Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Python 3.12 deprecated and removed the already deprecated and not documented imp library (the load_source function was never documented) Replace this with a modern alternative suggested by python/cpython#104212. Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Python 3.12 deprecated and removed the already deprecated and not documented imp library (the load_source function was never documented) Replace this with a modern alternative suggested by python/cpython#104212. Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Python 3.12 deprecated and removed the already deprecated and not documented imp library (the load_source function was never documented) Replace this with a modern alternative suggested by python/cpython#104212. Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Python 3.12 deprecated and removed the already deprecated and not documented imp library (the load_source function was never documented) Replace this with a modern alternative suggested by python/cpython#104212. Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Now that the
imp
removal has landed on main, users migrating to Python3.12 will likely need help to move toimportlib
.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 ofimp.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:SourceFileLoader(...).load_module()
, but this is also deprecated and slated for removal in 3.12 (according to the warning)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:
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 toimportlib
. IMO, we should do one of:importlib
What do you think?
Linked PRs
The text was updated successfully, but these errors were encountered: