[Python-Dev] Draft: PEP for imports

Guido van Rossum guido at python.org
Sun Jan 25 16:59:16 EST 2004


> Here's the draft PEP for imports.  I'll wait a few days for comments
> before sending to the PEPmeister.

I've seen a few suggestions of using slashes intead of dots, enabling
the use of ../ for relative import.  I believe these are misguided.
This would blur between the filesystem and the package namespace; next
people would be expecting to be able to say

  from /home/guido/lib/python import neatTricks

and then all hell breaks loose: what would the package name for
neatTricks be?  Should it be a toplevel module, 'neatTricks'?  Or
should it be called 'home.guido.lib.python.neatTricks'?  Neither is
acceptable.  So please don't include that in the PEP -- the idea is
dead on arrival.

[...]
> Here are the contenders:
> 
> *   One from Guido:
> 
> ::
> 
>     from .foo import
> 
> and
> 
> ::
> 
>     from ...foo import
> 
> These two forms have a couple of different suggested semantics.  One
> semantic is to make each dot represent one level.  There have been
> many complaints about the difficulty of counting dots.  Another
> option is to only allow one level of relative import.  That misses a
> lot of functionality, and people still complained about missing the
> dot in the one-dot form.  The final option is to define an algorithm
> for finding relative modules and packages; the objection here is
> "Explicit is better than implicit".

The PEP should specify the algorithm proposed (which is to search up
from the containing package until foo is found).

[...]
> *   Finally, some people dislike the way you have to change ``import``
> to ``from ... import`` when you want to dig inside a package.  They
> suggest completely rewriting the ``import`` syntax:
> 
> ::
> 
>     from MODULE import NAMES as RENAME searching HOW
> 
> or
> 
> ::
> 
>     import NAMES as RENAME from MODULE searching HOW
>         [from NAMES] [in WHERE] import ...

Nobody seems to have questioned this syntax.  But what does NAMES
stand for?  The current renaming syntax lets you write e.g.

  import foo as bar, spam as ham

It doesn't make sense to write

  import foo, spam as one_name

and

  import foo, spam as bar, ham

is definitely worse IMO (not to mention that this already has a
meaning today).

So then the two questions about the first example are:

- how string does the "searching HOW" clause bind?  Do you write

  import foo as bar searching XXX, spam as ham searching XXX

or

  import foo as bar, spam as ham searching XXX

???

Also, the PEP should mention the choices for HOW (I don't recall what
they are).  And your syntax should show which parts are optional, e.g.

  [from MODULE] import NAME [as RENAME], NAME [as RENAME], ... [searching HOW]

I'm totally lost with the second alternative.  Maybe examples of both
should be given, unless you want to abandon these right away (fine
with me :-).

Anyway, stick it in the PEP archive already.  You can still edit it
later. :-)

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list