[Python-Dev] Relative import

Guido van Rossum guido at python.org
Wed Dec 17 16:00:39 EST 2003

> > It's interesting that the "scan upwards on the package name" gets
> > so much support.

> I would actually be surprised if people who say they want "scan
> upward" actually want something else: they want to scan upward up to
> a limit.

There's a negation missing here, but I think I understand you.

> If you have a complex package with, say, three levels you may want
> to scan up through the levels of your own package.  However, if your
> package is later incorporated in a third party package I can hardly
> imagine that you want to scan in there too.

I think someone actually posted a use case: they were referring to a
global 3rd party module from within a package, and then later they
were moving the module into the package.  For both to work without
changes to the import statement the scan would have to go all the way

If we can disregard this use case, I still like my original proposal
better, because it is more explicit: you have to use the right
number of dots, and (like Unix paths starting with ../ or ../../ etc.)
it is still completely explicit what you are referring to (assuming
the current package/directory is known).

But I expect the ... version will work just as well in practice: in
debugged code, the ... refers to a particular ancestor, so
what's above that is irrelevant; and what's below that is typically
only two or at most three levels deep -- I should think a group of
cooperating package authors who can't keep a namespace of that size
coordinated will have bigger problems...

And the advantage of the ... version is that it's more conspicuous
than a single dot (for the most common case), and you don't have to
count dots.

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

More information about the Python-Dev mailing list