[Python-Dev] Relative import

Paul Svensson paul-python at svensson.org
Mon Dec 22 06:48:30 EST 2003

Summarizing what I've seen so far:

Status quo:

    [from NAME] import NAME [, NAME]
    [from NAME] import NAME as NAME

There also seems to be consensus on adding:

    [from NAME] import (NAME [, NAME])

To this, we want to as a way to spell out explicitly how the module is found;
for example:
    -a- by searching sys.path
    -b- in the current package
    -c- in the current package or its parent package, recursively
    -d- among the python standard library modules (and nowhere else)
    -e- in the current working directory
    -f- in a specific directory
    -g- combinations of the above

Jack Diederich proposed:

>> from MODULE import NAMES as RENAME searching HOW
>> or
>> import NAMES as RENAME from MODULE searching HOW

Guido van Rossum oppined:

>  from COGS import generate searching relative
>To me, that looks a lot like someone left the commas out of
>  from COGS import generate, searching, relative


>A separate suggestion is to switch from "from X import Y" to "import Y
>from X".  This seems a gratuitous change even if it reads or edits
>marginally better.  Now's not the time to tinker with marginal stuff
>-- that should've been done 10 years ago.

Not only a gratuitous change; it was always my impression that "from" was
at the beginning of the statement for exactly the reason Guido is hesitant
about adding "searching" at the end of it.  With this in mind, my suggestion
is to add the searching syntax should before the "import" keyword rather
than at the end of the statement.  Reusing an existing keyword, we get:

    [from NAMES] [in WHERE] import ...

WHERE would then be an object, or a list of objects (-g-) specifying the
import mechanism:

    -d- __stdlib__ or maybe stdlib
    -b- __package__
    -c- __search__
    -e- '.'
    -f- '/home/bellman/python'
    -a- sys.path

Without any "from" clause, "in WHERE import ..." reads a little strangely,
but I think it should be possible to get used to it, just as we've gotten
used to the current "from NAME import ..."

Would this work ?


More information about the Python-Dev mailing list