[Python-Dev] zipimport & import hooks

Fredrik Lundh fredrik@pythonware.com
Fri, 6 Dec 2002 16:09:03 +0100


kevin wrote:

> > quick question: would the following variation of feeling #2 be =
acceptable:
> >=20
> > >   2) They don't care if Python stores objects, strings, or bananas =
in
> > >      sys.path, so long as=20
> > >=20
> > >        sys.path=3Dmap(str,sys.path)=20
> >=20
> > +
> > +      on a sys.path set up by the Python interpreter itself
> >=20
> > >      results in a human-readable path and does not change how =
imports occur.
>=20
> I suppose, though the motivation behind it is not transparant to me.

under the proposed scheme, Python will never mess things up for you by
itself.  by default, and as long as you only use the standard =
mechanisms,
all items in the path behaves like strings, and point to things =
(directories
or archives) in the filesystem.

> There are many places in Python where a user can do fruity things, and
> the general response has been "DON'T DO THAT!".  Is your qualification
> an attempt to close such a loophole or is there a realistic use-case =
that
> is affected?

but what if I *want* to do fruity things?  that's the whole point here:
if the framework delegates imports to objects in the sys.path list, it's
dead simple to experiment.

for some reason, all counter-proposals seem to be obsessed with control
("don't do that" in lower or upper case, "don't write code that I may =
find
ugly", "we'll break your programs in the future if you do that", "if you
cannot think of a string representation, you're not qualified to write
custom importers", "all your ZIP archives must use the same name",
etc).

why are we spending this much time and effort on making things hard
to use?

if I know that I can access all modules needed by my program through
a five-line custom importer, why do I have to invent a string naming
scheme, make sure that scheme is unique so no future version of Python
may mistake it for a builtin importer, register the importer using some
function that I can remember the name and signature of, and pray I've
understood the semantics.

isn't it much easier, and much more obvious for anyone who's survived
Python 101, if I can just write

    sys.path =3D [myimporter()]

and leave early that day.

> For discussion, here is another use-case that I care about (a little):
>=20
>   Consider a PYTHONPATH includes a mix of filesystem paths, zip files, =
maybe
>   some tgz or tar.bz2 files.
>=20
>   We would expect that:
>=20
>     ':'.join(map(str,sys.path)) =3D=3D os.environ['PYTHONPATH']
>=20
>   modulo path normalization and any "extra" items that Python decided =
to
>   toss into sys.path.

the proposed scheme supports this for zip files.

you probably don't want to use tgz or tar.bz2 (no random access), but if =
you
insist, you just need to add 2-3 lines to your startup code to activate =
your
custom importer (Python only reads PYTHONPATH on startup anyway).

</F>