[Python-Dev] Re: PEP 328 -- relative and multi-line import

Greg Ewing greg at cosc.canterbury.ac.nz
Mon Apr 5 22:49:58 EDT 2004

Neil Schemenauer <nas-python at python.ca>:

> On Mon, Apr 05, 2004 at 01:36:24PM -0400, Aahz wrote:
> > Your intuition is wrong.  From package's __init__.py, ``from .`` refers
> > to package's parent; to get back into the package, you need to use
> > ``from .package``.
> That's confusing.  I expected '.' to refer to the current package,
> just as '.' refers to the current directory in most file systems.

Hmmm. I think people's intuition is likely to differ here depending on
whether the module doing the import is a plain module or a package.

In a plain module, the only sensible thing for .foo to mean is a
module foo in the same package as the importing module.

But in a package, it seems to me the most obvious meaning of .foo is a
module foo in the package. I would suggest that this is what it
*should* mean, since it's going to be very common for a package to
refer to submodules of itself, and requiring the name of the package
to be included in all such references would defeat one of the purposes
of having relative imports, which is to minimise the amount of
renaming needed when moving things around in the module hierarchy.

Would it be unreasonable to define "." as meaning the current
*package*, rather than the current *module* -- i.e. the package itself
if the importer is a package, and the containing package if it's a
plain module?

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+

More information about the Python-Dev mailing list