[Import-sig] Proposal for a modified import mechanism.

Prabhu Ramachandran Prabhu Ramachandran <prabhu@cyberwaveindia.com>
Sat, 10 Nov 2001 14:00:25 +0530


Sorry about the cross posting.

Over the last couple of weeks I described a problem and inconsistency
with the way Python imports modules.  For more details look at this


In short, currently, import allows one to use non-absolute module
names for modules that are in the current directory.  If a module is
not in the same directory, import then looks for modules in sys.path.
Consequently, dealing with packages that are re-nested is a pain.
Complex package structure also causes problems.  I'd like to note that
I was also not the only person who suffered from this issue -- four
others on mentioned similar problems and some asked me to let them
know if I found a solution.

Subsequently, I proposed another approach that first looks in the
local directory and then walks up the current package tree looking for
modules before looking at sys.path.  I also modified knee.py to obtain
a working solution.  More information is here:


the threading is messed up and starts here:


You can find the new module and a simple test package here:


There is also a slightly enhanced version of knee.py included that
supports caching module lookup failures suggested by Rainer and Gordon


it also fixes a bug where the parent package is an extension module.

I therefore have a working import style that seems to handle importing
packages in a more natural(?) and consistent(?) manner.  I've also
tested it out with a large package like scipy (http://www.scipy.org)
with no trouble or significant performance problems:


I'd like to ask the Python developers if they'd consider

    (a) changing the way the current import works to do what I
    proposed, or, 

    (b) add a new keyword like 'rimport' (or something) that does this
    recursive search through parent packages and loads modules.  This
    was actually suggested by Gordon McMillan.  Gordon actually
    suggested something stronger -- import only supports absolute
    names, rimport is relative import and rrimport is a recursive
    relative import.  But this would break the current import since
    import currently aupports some relative lookup.  So maybe import
    and rimport is a workable solution? 

    (c) patch the existing knee.py with my fixes.  Note: these fixes
    have nothing to do with the recirsive module lookup stuff --
    knee.py is merely an improved version of the older one.

Thanks for listening patiently and sorry again for all the cross