[Python-Dev] Relative import

Andrew Bennetts andrew-pythondev at puzzling.org
Wed Dec 17 22:04:09 EST 2003


On Wed, Dec 17, 2003 at 09:33:43PM -0500, Barry Warsaw wrote:
> 
> I'll just note that where the current status quo trips /me/ up most is
> when I accidentally have a local module with the same name as a global
> module, and then I write an import statement expecting to get the
> standard library module, but end up getting the local module.
> 
> That's why when I tend to think about this, I start wanting a way to
> spell "definitely give me the global one, no matter what".  IOW, I feel
> like I want a way to bypass relative module lookups.

Alternatively, maybe what you want is a way to say "definitely give me the
standard library one, no matter what", e.g.

    from stdlib import codecs

This would allow other packages to have their own codecs module/package
without worrying about confusing import errors.  To make this backwards
compatible, though, we'd probably need the equivalent of C++'s "using
namespace" statement, with no declaration being equivalent to:

    using namespace stdlib

Or perhaps more python-like:

    __import_base__ = 'stdlib'

Python 3.0 might default to __import_base__ = '', to force people to be
explicit.

Actually, the proposed with statement could be used here, e.g.

    import stdlib
    with stdlib:
        from . import codecs, user, gc

looks reasonably clear to me, and for long package paths, it could be very
convenient:

    import zope.app.interfaces
    with zope.app.interfaces:
        from .location import ILocation
        from .foo import IFoo, IBar
        # etc

-Andrew.




More information about the Python-Dev mailing list