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: Consider leaving importlib.abc.Loader.load_module()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: Arfrever, Borisd13, Claudiu.Popa, brett.cannon, eric.snow, martin.panter, ncoghlan, nikicat, srittau
Priority: normal Keywords:

Created on 2014-05-05 12:00 by srittau, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg217919 - (view) Author: Sebastian Rittau (srittau) * Date: 2014-05-05 12:00
It was very easy to load plugin files in Python 2:

import imp
my_module = imp.load_source("what.ever", "foo.py")

Unfortunately, this became much more obscure in Python 3.3:

import importlib.machinery
loader = importlib.machinery.SourceFileLoader("what.ever", "foo.py")
my_module = loader.load_module("what.ever")

In Python 3.4 even this has been deprecated. There should be a way (preferable an easy-to-use one) to load a Python module by filename or by stream.
msg217923 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2014-05-05 13:46
So it's not quite as bad as you think as SourceFileLoader.load_module() doesn't need an argument (I've opened http://bugs.python.org/issue21438 to fix the documentation). Admittedly it is a longer command than imp.load_source() to type, but there is no extra information required or a necessity that you break the command up into multiple lines.

Plus imp.load_source() is just plain bad. The reason the imp module is deprecated in Python 3.4 is because it does not expose the low-level details of import in a way that makes any sense since Python 2.3 (and yes, I meant to write 2.3 instead of 3.3; the problem has persisted _that_ long).

That being said, talks are just starting to consider undoing the documented deprecation of load_module() such that you can continue to use that as a substitute for imp.load_source()/imp.load_module().

I'm going to leave this bug open, hijack its title, and refocus this as to consider leaving importlib.abc.Loader.load_module() in importlib as the all-powerful fallback API which also simplifies transitioning from imp.
msg219134 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2014-05-26 05:55
I'd rather see something like "load_from_spec()" added to importlib.util, a la issue #21235.
msg255901 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2015-12-04 23:56
Python 3.5 lets you do:

  spec = importlib.util.spec_from_file_location('what.ever', 'foo.py')
  module = importlib.util.module_from_spec(spec)
  spec.loader.exec_module(module)

I am satisfied that case for loading from a file is easy enough to not warrant keeping load_module() around just for this use case.
msg405715 - (view) Author: Sebastian Rittau (srittau) * Date: 2021-11-04 12:35
I would ask you to reconsider this. https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path/67692#67692 is a highly active question on StackOverflow, and my answer basically provided me all the karma I got there. For users that don't have intimate insight in how importlib works, the code posted by Brett is completely non-obvious and I believe that a convenience method would be very useful.
msg405919 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2021-11-08 00:51
FWIW, I think it would be desirable to retain/restore some form of API that allows the creation of modules from files without requiring the user to know about module specs (or loaders, or anything else).

My current preference would be for a "module_from_file_location" counterpart to "spec_from_file_location" that implements Brett's recipe from above. That code is visually short, but conceptually very dense,and hence far from being obvious to most people trying to make the "path to module" leap.

Such a function could also cross-reference runpy.run_path for cases where don't want a module at all, just the top level namespace.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65635
2021-11-08 00:51:22ncoghlansetmessages: + msg405919
2021-11-04 12:35:05srittausetmessages: + msg405715
2015-12-13 00:30:07berker.peksagsetstatus: open -> closed
2015-12-04 23:56:46brett.cannonsetresolution: rejected
messages: + msg255901
stage: resolved
2014-10-18 05:07:25martin.pantersetnosy: + martin.panter
2014-10-15 19:55:41nikicatsetnosy: + nikicat
2014-05-26 05:55:03eric.snowsetmessages: + msg219134
2014-05-13 00:31:38Borisd13setnosy: + Borisd13
2014-05-11 12:05:41Arfreversetnosy: + Arfrever
2014-05-05 13:46:59brett.cannonsetnosy: + ncoghlan
title: bring back importlib.load_source() et al. -> Consider leaving importlib.abc.Loader.load_module()
messages: + msg217923

assignee: brett.cannon
2014-05-05 12:21:32berker.peksagsetnosy: + brett.cannon, eric.snow
2014-05-05 12:08:29Claudiu.Popasetnosy: + Claudiu.Popa
2014-05-05 12:00:08srittaucreate