[Python-Dev] Alternative Approach to Relative Imports

M.-A. Lemburg mal@lemburg.com
Wed, 22 Sep 1999 14:47:03 +0200

Hi everybody,

I'm currently busy converting my code to use mx.DateTime instead
of just DateTime and must say that this is a real pain. Now instead
of reviving the __ discussion I'd like to turn to what could be
a workable compromise (I think Jim proposed something along these
lines already and there was similar code in ni.py).

The proposal is really only an addition to the lookup scheme
used by the importer. No additional syntax is involved and
best of all, it is backward compatible...

The current lookup does the following if you want to import
a module E from module A.B.C.D:

1. check A.B.C.E
2. check E
3. fail

Now instead of failing we could add a lookup method that
walks up the package structure:

3. check A.B.E
4. check A.E
[5. check E -- already done]
6. fail

so that the complete scheme looks like this:

1. check A.B.C.E
2. check E
3. check A.B.E
4. check A.E
[5. check E -- already done]
6. fail

That way I could leave intra-mx-package imports untouched and still
have the convenience of achieving the goal of making my mx
subpackages work in the mx context *plus* in the top-level
context thus allowing a backward compatible and flexible setup
for mx* users.

Note that the scheme finds exactly the same modules it did
previously, plus perhaps some more (which is intended), and
it does not involve any search path hacks.

How is that for a compromise ? [Ducking for cover ;-)]

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