[Import-SIG] How best to replace imp.load_module()?

Brett Cannon bcannon at gmail.com
Fri Apr 4 20:57:52 CEST 2014


I've been thinking about what it takes to replace imp and I realized that
imp.load_module() is the hardest to replace for two reasons. One issue is
that importlib.abc.create_module() can -- and does -- return None. This
means that if someone wanted to replicate the
imp.find_module()/imp.load_module() dance in a PEP 451 world it takes::

  spec = importlib.find_spec(name)
  try:
    module = spec.loader.create_module(spec)
  except AttributeError:
    module = None
  if module is None:
    module = types.ModuleType(spec.name)
  # No clear way to set import-related attributes.
  spec.loader.exec_module(module)

It took 6 lines to get a module. That seems a bit excessive and ripe to
either have an importlib.util function that handles this all correctly or
simply make create_module() a required method on a loader and have
importlib.abc.create_module() return types.ModuleType(spec.name).

The second annoyance is that we have not exposed
_SpecMethod.init_module_attrs() in any way. That can either come through in
importlib.util or we can make types.ModuleType grow a method that takes a
spec and then sets all the appropriate attributes. If we go with the former
we should make sure that in importlib we always prefer __spec__ over any
module-level values and that one can pass in a spec to types.ModuleType to
set __spec__ so that in the distant Python 4 future we can deprecate all
module-level attributes and just work off of __spec__.

I think if we can get these two bits cleaned up we can tell people who use
imp.load_module() directly that they can::

  # Assume proper loader chosen.
  spec = importlib.util.spec_from_loader(loader)
  module = some_newfangled_way_of_doing_this()
  somehow_init_module_attrs(module)
  loader.exec_module(module)

which isn't that bad for something they probably should be avoiding as much
as possible to begin with.

Or we take the easiest option and simply ignore all of these issues and
just say that working outside of import is not something we want to worry
about in the stdlib and let PyPI come up with some utility code that does
all of this for you if you really want it. The code maintainer in me is
liking this idea + making it easier to set __spec__ on a module through its
constructor.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/import-sig/attachments/20140404/6cffde4a/attachment.html>


More information about the Import-SIG mailing list