[Python-Dev] Relative Package Imports

M.-A. Lemburg mal@lemburg.com
Mon, 13 Sep 1999 12:17:31 +0200

Gordon McMillan wrote:
> M.-A. Lemburg wrote:
> > Guido van Rossum wrote:
> > >
> > > I'd much rather use absolute package names for anything that's not
> > > in the same directory as the current module.
> >
> > Of course, you could do everything with absolute names, but then
> > the package author would dictate the complete absolute path which is
> > not always desirable since it can cause name collisions such as
> > DateTime in mxDateTime and Zope or Queue in mxQueue (to be released)
> > and Mark's win32 stuff.
> I can see your point (although I also believe that authors - Guido
> excepted - should come up with collision-free names, probably in a
> Java-ish scheme).

> But I strongly believe that import.c should be left alone, maybe
> even to die. There are too many people doing import hooks to
> make fiddling with its behavior safe.
> I'm also a strong proponent of Greg's imputil.py scheme, which
> makes it a breeze to do import hooks. And my experience
> disproves the notion that the import mechanism needs to be in C. If
> you don't believe me, try the ZlibArchive stuff (which is cross
> platform) from my Win32 installer stuff. You can pack the standard
> library into one 475K file, and get a perceptible performance boost.

You're probably right in saying that we don't need the code in C.
I just wanted to avoid yet another import hook being incompatible
with all the other existing hooks.

Perhaps we should restart the import discussion all over and
come up with a more flexbile 100% compatible framework based
on Greg's imputil scheme. Then I could add my hook for the relative
imports and be happy ;-)
> OTOH, I could see doing a framework of packages, in which case
> relative imports might be handy. This seems to work fine:
> def relimp(nm):
>   rpth = string.split(nm, '/')
>   tpth = string.split(__name__, '.')[:-1]
>   for node in rpth:
>     if node == '..':
>       del tpth[-1]
>     else:
>       tpth.append(node)
>     #print `tpth`
>   return __import__(string.join(tpth, '.'))
> b = relimp('../packageA2.b')

This is pretty much how my patch works... except that I use the
ni.py style '__' pseudo package instead of '../'.

Marc-Andre Lemburg
Y2000:                                                   109 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/