[Python-Dev] Write All New Import Hooks (PEP 302) in Python, Not C

Guido van Rossum guido@python.org
Sat, 28 Dec 2002 08:48:24 -0500


> Samuele Pedroni wrote:
> 
> > I agree with Guido that special cookies can be used for normal
> > importers.

[Just]
> I find this rather ugly, and would only do that if it's crucial that the
> pathless importer should be invoked somewhere in the middle of sys.path.
> And I don't have a use case for that. There are plenty of use cases for
> sys.meta_path.

Sure, but pleading for cookies is that they allow easier user control
through PYTHONPATH, and/or through sys.path manipulation; cookies
allow you to control precisely the import order.

It seems they are indeed different features.  Once we have path_hooks,
cookies are a simple consequence; meta_path is an orthogonal feature.

I agree with Samuele's concern that if you have multiple meta_path
importers, ordering them may become an issue.  On the other hand,
ordering of entries in sys.path is a bit of a black art, but in 99% of
the cases, insert-in-front or append-at-end seem suffiecient.  I
expect that the same will be true for sys.meta_path.  (Though it would
be a bit more convenient if indeed it included the built-in, frozen
and path importers -- maybe we can get that done in alpha 2.)

I agree with Just that either of these hooks is *much* more convenient
than overriding __import__ -- because with __import__, you have to
reimplement the whole
look-in-sys.path-try-relative-try-absolute-import-parent-packages
routine, unless you have a truly trivial use (like printing a log
message and calling the real __import__).

--Guido van Rossum (home page: http://www.python.org/~guido/)