[Python-Dev] zipimport & import hooks

Martin v. Löwis martin@v.loewis.de
06 Dec 2002 14:51:49 +0100


Kevin Jacobs <jacobs@penguin.theopalgroup.com> writes:

>   for filename in os.listdir(__path__[0]):
>     if not filename.endswith('.py') or filename.startswith('__'):
>       continue
[...]
> Clearly, the above code can be redone in a much nicer way that doesn't
> depend on __path__, the qualified base package name, and __import__.

That would break only if the package becomes part of a zipfile, right?
And, if it is in a zipfile, it breaks no matter what mechanism we find
for zipfile imports, since it relies on os.listdir to obtain the
package contents, right?

>   1) They all are very convinced that
> 
>        sys.path.append('/usr/path/stuff.zip') 
> 
>      should work just like
>      
>        > mkdir /usr/path/stuff
>        > cd /usr/path/stuff
>        > unzip -d stuff.zip
> 
>        sys.path.append('/usr/path/stuff')

I think both Jim's and Just's patch meet that requirement; Gustavo's
plan doesn't.

>   2) They don't care if Python stores objects, strings, or bananas in
>      sys.path, so long as 
> 
>        sys.path=map(str,sys.path) 
> 
>      results in a human-readable path and does not change how imports occur.

For Just's patch, that is only true for zipfile imports. If you would
also provide URL, or .tgz import with his patch, then you'll loose the
magic when converting the path back to strings.

>   3) They all want ZIP archives to be a builtin feature, but don't care if
>      they have to write an extra line of code to register a TAR/BZ2/whatever
>      import hook.

Clearly, with Jim's patch, they'ld only get zipfile import, period.

> Hope this helps,

It sure does.

Thanks,
Martin