[Import-SIG] My objections to implicit package directories

Calvin Spealman ironfroggy at gmail.com
Thu Mar 15 12:26:55 CET 2012

On Mar 14, 2012 11:56 PM, "Guido van Rossum" <guido at python.org> wrote:
> On Wed, Mar 14, 2012 at 8:33 PM, PJ Eby <pje at telecommunity.com> wrote:
> > On Mon, Mar 12, 2012 at 11:49 PM, Guido van Rossum <guido at python.org>
> >>
> >> So the only backwards incompatibility is that "import foo" may succeed
> >> where it previously failed if there is a directory foo/ somewhere on
> >> sys.path but no foo.py and no foo/__init__.py anywhere. I don't think
> >> this is a big deal.
> >
> >
> > Actually, it *is* a big deal.  An early draft of PEP 402 was like the
> > newly-proposed approach, and it was shot down by an *actual code sample
> > a real package* that somebody posted to show the problem.  The
> > example is actually presented in the PEP: trying to "import json" with a
> > json/ directory present -- and the "json" package *not* present -- and
> > trying to use its contents.  The PEP works around this by taking the
> > namespace concept to its logical extreme and saying you can only import
> > *contents* of the namespace, because the namespace itself doesn't exist.
> >  (i.e., it's virtual.)
> >
> > tl;dr version: the killer problem with allowing "import foo" to succeed
> > that code which does try/except ImportError to test for a package's
> > will get false positives.  That's the motivating reason for only
> > sub-namespace package imports to succeed: it prevents the immediate
> > of real code, the moment the feature is introduced.  :-(
> Well, too bad. It's too much of a wart. Such try/except clauses make
> me sad anyway -- and we're trying to get rid of them by e.g.
> encouraging the pattern where heapq.py tries to import _heapq rather
> than having user code try first _heapq and then heapq. (And the idiom
> used in heapq.py doesn't mind if _heapq exists as an empty package.)

While I agree it is a wart, the heapq.py example might not be the best. A
more real-world example might be the situation is PEP 417, where we'll be
seeing people write

    import unittest.mock
except ImportError:
    import mock

And owning neither the two modules, the _ trick is not available.

I like the idea that if a package is only an implied namespace package,
with no contents of it's own, it cannot itself be directly imported. This
still allows the simple check, but without seriously modifying the new
feature itself.

> --
> --Guido van Rossum (python.org/~guido)
> _______________________________________________
> Import-SIG mailing list
> Import-SIG at python.org
> http://mail.python.org/mailman/listinfo/import-sig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/import-sig/attachments/20120315/06ae6fc8/attachment.html>

More information about the Import-SIG mailing list