[Import-SIG] New PEP draft: "Simplified Package Layout and Partitioning"

Nick Coghlan ncoghlan at gmail.com
Thu Aug 18 01:12:39 CEST 2011

On Thu, Aug 18, 2011 at 6:53 AM, P.J. Eby <pje at telecommunity.com> wrote:
> You might be right.  My concern, though, is that whenever you try to import
> a non-existent module, you'll be adding its parents to sys.modules --
> something that doesn't happen right now, because the import fails as soon as
> the parent isn't found.
> IOW, right now if I import foo.bar.baz, and there's no "foo", nothing gets
> added to sys.modules.  Under PEP 402 without lazy module creation, you'd end
> up with an emtpy 'foo' pointing to 'bar', and an empty 'foo.bar' module,
> despite the possible non-existence of these packages.

Well, you'd only add the pure virtual packages if they had a non-empty
__path__, so they'd exist in at least *some* sense (i.e. there is at
least one subdirectory with an appropriate name available via sys.path
or the relevant parent __path__ attribute).

To use the example from the PEP, all of the following would create a
pure virtual 'json' entry in sys.modules, even though the last one
doesn't find the requested child module:

  import json.foo
  import json.bar
  import json.baz

On the other hand, something like the following wouldn't touch
sys.modules, since it would fail to find any directories for the
parent name and the generation of the pure virtual package itself
would fail:

  import notadir.foo


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Import-SIG mailing list