relative import broken?

Leo Kislov Leo.Kislov at gmail.com
Fri May 4 02:13:12 EDT 2007


On May 3, 10:08 am, "Alan Isaac" <ais... at american.edu> wrote:
> "Alex Martelli" <a... at mac.com> wrote in message
>
> news:1hxcdkq.47p2r6beuctcN%aleax at mac.com...
>
> > Very simply, PEP 328 explains:
> > """
> > Relative Imports and __name__
>
> > Relative imports use a module's __name__ attribute to determine that
> > module's position in the package hierarchy. If the module's name does
> > not contain any package information (e.g. it is set to '__main__') then
> > relative imports are resolved as if the module were a top level module,
> > regardless of where the module is actually located on the file system.
> > """
>
> To change my question somewhat, can you give me an example
> where this behavior (when __name__ is '__main__') would
> be useful for a script? (I.e., more useful than importing relative
> to the directory holding the script, as indicated by __file__.)

Do you realize it's a different behaviour and it won't work for some
packages? One possible alternative is to assume empty parent
package and let from . import foo work but not
from .. import bar or any other upper levels. The package author
should also realize __init__.py will be ignored.

  -- Leo




More information about the Python-list mailing list